안녕하세요 여러분, 프로그램 자체에서 묻는 단어를 사용자가 추측해야하는 프로그램이 있습니다. 코드에는 구문 오류가 없지만 요청할 때마다 올바른 단어를 입력 할 때마다 JOptionPane (ErrorMessage)이 계속 표시됩니다.while 루프 오류가 발생합니다.
내가 원했던 것은 사용자가 마지막 시도에서 잘못된 단어를 입력하면 5 번만 시도 할 수 있다는 것입니다. 요청한 단어가 올바르게 표시되어야합니다. 사용자가 올바른 단어를 입력하면 다음 단어로 이동해야합니다. 이걸 고치는 걸 도와주세요. 벌써 3 시간 동안 여기 붙어 있습니다. 고맙습니다.
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));
}
isCorrect의 코드를 볼 수 있습니까? 그것은 항상 false를 반환 할 수 있습니다. –
나는 equals() 대신에 ==를 사용하여 문자열 비교에 5 달러를 투입 할 것입니다! –
... 또는 equalsIgnoreCase() ... – vefthym