简体   繁体   中英

Do while loop error

Hey guys I have here a program wherein a user needs to guess the word which is being asked by the program itself. The codes doesn't have syntax errors, but my problem here is that every time you input the correct word that is being asked for, the JOptionPane (ErrorMessage) still appears.

What I want to happen is that, the user can only have 5 trials, once the user entered a wrong word at the last trial given, it should display the correct word that is asked for. And once the user entered the correct word, it should go to the next word. Please help me fix this I'm stuck in here for like 3 hours already. Thank you very much.

private void guessedWordActionPerformed(java.awt.event.ActionEvent evt) {                                            
        int trials = 5;
        boolean tryAgain = true;

        do{

        if (wordLibrary.isCorrect(wordIdx, guessedWord.getText())){
            JOptionPane.showMessageDialog(null, "Your answer is correct! Guess another word.","", JOptionPane.INFORMATION_MESSAGE);
            getRootPane().setDefaultButton(nextTrial);
            guessedWord.setText("");

            wordIdx = (wordIdx + 1) % wordLibrary.getSize();
            scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
            guessedWord.setText("");
            getRootPane().setDefaultButton(guessButton);
            guessedWord.requestFocusInWindow();
            tryAgain = false;
            }
        else if (!wordLibrary.isCorrect(wordIdx, guessedWord.getText())) {
            JOptionPane.showMessageDialog(null, "Your answer " + guessedWord.getText() + " is wrong.\n   Number of trials remaining: " + trials ,
                    "Incorrect Answer", JOptionPane.ERROR_MESSAGE);
            trials--;
            guessedWord.setText("");
            tryAgain = true;
        }
        }while(tryAgain && trials > 0);

        guessedWord.requestFocusInWindow();
    }

//This is the isCorrect method
public boolean isCorrect(int idx, String userGuess) {
        return userGuess.equalsIgnoreCase(getWord(idx));
    }

This is happening in your action performed. When you loop, you're not giving the user any time to enter new information.

Why do you want to loop here? You don't need it. Just check once. If they're wrong change the components and wait for ActionPerformed to be called again.

If you want to give a maximum number of trials, then you should use some form non-local variable to store it.

When you first give a wrong answer, guessedWord's text becomes the empty String "", so at the next iteration, it will never be equal to the given word, because the String that you get with guessedWord.getText() will now be "".

You need to ask the user for a new word and then get the NEW word!

For example, you could set a private variable int trials in your class, initialized with 5 (in your main method) and another one, boolean tryAgain initialized with true. Then the above method could be written as:

private void guessedWordActionPerformed(java.awt.event.ActionEvent evt){          

  if (tryAgain && trials > 0) {                                  
    if (wordLibrary.isCorrect(wordIdx, guessedWord.getText())){
        JOptionPane.showMessageDialog(null, "Your answer is correct! Guess another word.","", JOptionPane.INFORMATION_MESSAGE);
        getRootPane().setDefaultButton(nextTrial);
        guessedWord.setText("");

        wordIdx = (wordIdx + 1) % wordLibrary.getSize();
        scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
        guessedWord.setText("");
        getRootPane().setDefaultButton(guessButton);
        guessedWord.requestFocusInWindow();
        tryAgain = false;
    } else {
        trials--;
        JOptionPane.showMessageDialog(null, "Your answer " + guessedWord.getText() + " is wrong.\n   Number of trials remaining: " + trials ,
                "Incorrect Answer", JOptionPane.ERROR_MESSAGE);            
        guessedWord.setText("");
        tryAgain = true;
    }

  } else {
      //show "the correct word was..."
  }
  guessedWord.requestFocusInWindow();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM