【发布时间】:2014-03-17 15:08:55
【问题描述】:
===== CS302 TOOL BOX =====
T > COIN TOSS SIMULATOR
G > GRADE ESTIMATOR
C > COLOR CHALLENGE
Q > QUIT
Type code letter for your choices: h
Invalid selection. Please try agian:
t
Invalid selection. Please try agian:
T
^这是我运行程序并给出错误猜测时得到的结果。循环是为了验证用户输入,所以它完全失败了。关于我做错了什么的任何想法?感谢您的宝贵时间!
}
System.out.println("===== CS302 TOOL BOX =====");
System.out.println("T > COIN TOSS SIMULATOR");
System.out.println("G > GRADE ESTIMATOR");
System.out.println("C > COLOR CHALLENGE");
System.out.println("Q > QUIT");
System.out.print("Type code letter for your choices: ");
boolean code_letter;
String code_choice = scanner.next();
do {
if (code_choice.toUpperCase().equals("Q")
|| (code_choice.toUpperCase()).equals("C")
|| (code_choice.toUpperCase()).equals("G")
|| (code_choice.toUpperCase()).equals("T")) {
code_letter = true;
}
else {
System.out.println("Invalid selection. Please try agian: ");
code_letter = false;
scanner.next();
}
} while (!(code_letter));
{
System.out.println("you did it?");
}
}
}
【问题讨论】:
-
请不要在得到答案后用垃圾文字代替您的问题。将问题留给未来的读者学习。
-
如果某个答案对您有所帮助,请单击旁边的复选标记接受该答案是有礼貌的。 stackoverflow.com/help/accepted-answer
标签: java validation input while-loop