===== 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
^這就是我在運行程序並給出錯誤的猜測時所得到的結果。該循環用於驗證用戶輸入,因此它完全失敗。關於我做錯了什麼想法?謝謝你的時間!輸入驗證(do-while循環)出現故障
}
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?");
}
}
}
請不要在獲得答案後用垃圾文本替換您的問題。留下問題讓未來的讀者學習。 –
如果答案對您有幫助,點擊旁邊的複選標記以接受答案是禮貌的。 http://stackoverflow.com/help/accepted-answer –