我正在尝试创建一个程序,该程序将使用名为“防弹”的程序包从用户那里获取输入(完成,工作正常),但是当我找到匹配项时出现无限循环错误。我有点不知所措,我觉得我不太理解我的代码为什么不起作用。我不是在寻找直接的答案,因为我正在努力学习这一点,但任何帮助都将不胜感激。谢谢,我的代码如下。
--edit-- 我得到了程序的工作。谢谢大家的帮助,你们太棒了。
import bulletproof.*;
public class A26_1 {
public static void main(String[] args) {
BPScanner kb = new BPScanner();
String reservedWordToCheck = kb.getStringFromUser("Enter a word to see if it's reserved or enter leave: ");
System.out.println(ReservedWordChecker(reservedWordToCheck));
}
public static String ReservedWordChecker(String reservedWordToCheck) {
String[] table = {
"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "enum", "extends", "final", "finally", "float",
"for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native",
"new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while"
};
while (true) {
if (reservedWordToCheck.equalsIgnoreCase("leave"))
break;
boolean found = false;
for (int i=0; i < table.length; i++) {
if (reservedWordToCheck.equalsIgnoreCase(table[i])) {
found = true;
break;
}
if (found)
System.out.println("Yeah, " + reservedWordToCheck + ", is reserved as a Java identifier.");
else
System.out.println("Nope, the word " + reservedWordToCheck + ", isn't reserved. Go hog wild.");
}
System.out.println("OK BYE");
} return reservedWordToCheck;
}
}