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。我该如何解决?