public static void operation() {
try {
Scanner input = new Scanner(System.in);
String choiceString = "";
char choice = 'a';
System.out.print("Enter letter of choice: ");
choiceString = input.next();
if (choiceString.length() == 1) {
choice = choiceString.charAt(0);
System.out.println("-------------------------------------------");
switch(choice) {
case 'a': {
....
break;
}
case 'b': {
....
}
default:
throw new StringException("Invalid choice...");
}
}else {
throw new StringException("Invalid input...");
}
} catch(StringException i) {
System.out.println("You typed " + choiceString + i);
}
當程序提示用戶輸入選擇的字母,並且用戶輸入一個單詞或數字時,它應該捕獲異常。它應該顯示這個輸出:異常處理錯誤:無法找到符號變量
You typed: ashdjdj
StringException: Invalid input...
這裏的問題是,它找不到變量choiceString。我該如何解決?
你夥計們,非常感謝你們!我不知道那件事。我的程序現在完美工作,謝謝你一百萬次! – Ciara
接受答案是個好主意:) – Suranga