java接受大写字符串输入
我有一个游戏程序,要求用户猜出给定的加扰词
例如: 加扰词:loglab 你的猜测是:
这个词是全局的,每次我输入“global”,它都会说这个词不正确。我试着用toUpperCase,但没用。我的程序怎么能接受输入的字符串,即使它是大写的?下面是我的一些代码。我不会把它全部贴出来,因为它太长了,但是如果你需要更多的信息,请告诉我。请帮我处理这件事,谢谢
静态字库。爪哇:
public boolean isCorrect(int idx, String userGuess) {
return userGuess.equals(getWord(idx));
}
字库。爪哇:
public abstract boolean isCorrect(int idx, String userGuess);
字谜。爪哇:
private void guessedWordActionPerformed(java.awt.event.ActionEvent evt) {
if (wordLibrary.isCorrect(wordIdx, guessedWord.getText())){
JOptionPane.showMessageDialog(null, "Your answer is correct! Guess another word.","", JOptionPane.INFORMATION_MESSAGE);
getRootPane().setDefaultButton(nextTrial);
} else {
JOptionPane.showMessageDialog(null, "Your answer is incorrect! Please try again.","", JOptionPane.ERROR_MESSAGE);
guessedWord.setText("");
}
guessedWord.requestFocusInWindow();
}
# 1 楼答案
用
equalsIgnoreCase()
代替equals()
# 2 楼答案
你喜欢这样吗
或者
# 3 楼答案
因为单词是
"global"
(小写),输入是大写,所以应该使用方法userGuess.toLowerCase()
而不是toUpperCase()
另一种方法是与
equalsIgnoreCase()
进行比较