lundi 25 mai 2015

While loop running incorrect number of times

I am having trouble with a while loop, as the if statement is meant to check if the button is empty if not repeat. However if the whole if statement is in a while loop it reveals about 4 buttons after running the code for the first time.

case HINT:
    Random random = new Random();
    int size = SizeOfPuzzle.currentRows;
    int randomNumber1 = random.nextInt(size);
    int randomNumber2 = random.nextInt(size);
    if (puzzlePanel.arrayOfButtons[randomNumber1][randomNumber2].getText().isEmpty()) {
       puzzlePanel.arrayOfButtons[randomNumber1][randomNumber2].setText(puzzle.getSingleLetter(randomNumber1, randomNumber2) + "");
    }else {
       boolean finished = true;
       while (!finished) {
          randomNumber1 = random.nextInt(size);
          randomNumber2 = random.nextInt(size);
          if (puzzlePanel.arrayOfButtons[randomNumber1][randomNumber2].getText().isEmpty()) {
              puzzlePanel.arrayOfButtons[randomNumber1][randomNumber2].setText(puzzle.getSingleLetter(randomNumber1, randomNumber2) + "");
              break;
          }

       }
    }
break;




Aucun commentaire:

Enregistrer un commentaire