0

In this code, when a button is pressed, it shows a random number. I don't want that number to repeat itself. I have it so that when a big button is pushed, a number generates 1-10. I want it so that IF that number is duplicated it runs the code again to find another number that works. I've been working on it for a while and tried a lot of things. Any tips/tricks would be great. Thank you.

class HiLow extends JFrame implements ActionListener{
    int NumberGenerated;
    JButton Push, High,Low, Bank;
    JLabel Score;
    int helpInt = 0;
    int counterInt = 0;
    ArrayList<Integer> numberList = new ArrayList<Integer>();
    int size = numberList.size();
    boolean hp = true;

    HiLow(){
        JFrame l = new JFrame();
// JOptionPane.showMessageDialog(l, "Press the 'Push' button\nThen look at the number rolled\nThere will be numbers from 1-10\nThen click higher or lower\nYou can bank your points using the bank button\nMost points after 5 spins wins\nGood luck!");
        Push = new JButton("Push For Number");
        High = new JButton("Higher");
        Low = new JButton("Lower");
        Bank = new JButton("Bank Points");
        Score = new JLabel("");

        add(Push);
        add(High);
        add(Low);
        add(Bank);
        add(Score);

        Push.addActionListener(this);
        High.addActionListener(this);
        Low.addActionListener(this);
        Bank.addActionListener(this);

        Push.setBounds(150,150,200,200);
        High.setBounds(10,420,100,50);
        Low.setBounds(390,420,100,50);
        Bank.setBounds(10,10,100,50);
        Score.setBounds(205,100, 150,50);

        High.setEnabled(false);
        Low.setEnabled(false);

        setLayout(null);
        setSize(500, 500);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==Push){
            helpInt++;
            int max = 10;
            int min = 1;
            int range = max - min + 1;
//         if(size >= 5) {
//             High.setEnabled(false);
//             Low.setEnabled(false);
//             Push.setEnabled(false);
//         }
            do {
                int NumberGenerated = (int)(Math.random() * range) + min;
                if(numberList.contains(NumberGenerated)) {
                    High.setEnabled(false);
                    Low.setEnabled(false);
                    Push.setEnabled(true);
                    invalidate();
                    validate();
                    repaint();
                }else {
                    String score = String.valueOf(NumberGenerated);
                    Score.setText("You Rolled: " + score);
                    numberList.add(NumberGenerated);
                    System.out.println(numberList);
                    hp=false;
                }
            }while(hp == true);
            invalidate();
            validate();
            repaint();
            if(helpInt == 1) {
                High.setEnabled(true);
                Low.setEnabled(true);
                Push.setEnabled(false);
            }
        }
////////////////
        if(e.getSource()==High){  
            helpInt--;
            counterInt++;
            if(helpInt == 0) {
                High.setEnabled(false);
                Low.setEnabled(false);
                Push.setEnabled(true);
            }
//if(size >= 5) {
//     High.setEnabled(false);
//     Low.setEnabled(false);
//     Push.setEnabled(false);
//}
        }

////////////////

        if(e.getSource()==Low){
            helpInt--;
            counterInt++;
            if(helpInt == 0) {
                High.setEnabled(false);
                Low.setEnabled(false);
                Push.setEnabled(true);
            }
//if(size >= 5) {
//     High.setEnabled(false);
//     Low.setEnabled(false);
//     Push.setEnabled(false);
//}
        }

////////////////

    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
Parda
  • 31
  • 6

0 Answers0