如何修复switchcase语句中的异常处理问题



我正在为班级做项目,我们的任务是设计一个计算器程序,该程序的菜单包含5个选项。如果用户输入了一个不在1和5之间的选项,当我试图编写代码以捕获时,我会遇到一个问题。当前,如果用户输入一个介于6到9之间的数字。异常将在第一次被捕获,并显示一条错误消息,说明输入1和5之间的选项,并显示重新输入的消息。但是,如果用户继续输入6到9之间的数字,则不会显示错误消息,并显示主菜单。我还试图捕捉何时输入字符串作为输入而不是1和5之间的选择,并显示不同的错误消息,说明用户输入了无效输入,然后要求用户重新输入,然而,当输入字符串作为选择时,我会收到输入不匹配异常错误,但当选择操作后输入字符串而不是浮点值时,则显示正确的错误消息。

我是Java的初学者,对所有的建议都持开放态度,但如果可能的话,我希望我的代码与当前的编写方式保持一定的相似性。

static void promptEnterKey() {
System.out.println("Press enter key to continue ...");
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
}
public static void main(String[] args) {
float Firstnum, Secondnum, Solution;
int choice;
Scanner scan = new Scanner(System.in);
do {
System.out.printf("Welcome to Paul's Handy Calculatornn (1) Additionn "
+ "(2) Subtractionn (3) Multiplicationn (4) Divisionn (5) Exitnn");
System.out.printf("What would you like to do? ");
choice = scan.nextInt();
try {
if (choice < 1 || choice > 5) {
System.out.printf("You have not entered a number between 1 and 5. "
+ "Try again.n");
System.out.printf("Enter your choice between 1 and 5 only: n");
choice = scan.nextInt();
continue;
}
switch (choice) {
case 1:
System.out.print("Please enter two floats to add, "
+ "separated by a space: ");
Firstnum = scan.nextFloat();
Secondnum = scan.nextFloat();
Solution = Firstnum + Secondnum;
System.out.println("Result of adding " + Firstnum + " and " 
+ Secondnum + " is " + Solution + "n");
promptEnterKey();
break;
case 2:
System.out.println("Please enter two floats to subtract, "
+ "separated by a space: ");
Firstnum = scan.nextFloat();
Secondnum = scan.nextFloat();
Solution = Firstnum - Secondnum;
System.out.println("Result of subtracting " + Firstnum 
+ " and " + Secondnum + " is " + Solution + "n");
promptEnterKey();
break;
case 3:
System.out.print("Please enter two floats to multiply, "
+ "separated by a space: ");
Firstnum = scan.nextFloat();
Secondnum = scan.nextFloat();
Solution = Firstnum * Secondnum;
System.out.print("Result of multiplying " + Firstnum + " and " 
+ Secondnum + " is " + Solution + "n");
promptEnterKey();
break;
case 4:
System.out.print("Please enter two floats to divide, "
+ "separated by a space: ");
Firstnum = scan.nextFloat();
Secondnum = scan.nextFloat();
if (Secondnum == 0) {
System.out.println("You cannot divide by zero, "
+ "please enter another number to divide by");
Secondnum = scan.nextFloat();
}
Solution = Firstnum / Secondnum;
System.out.println("Result of dividing " + Firstnum + " and " 
+ Secondnum + " is " + Solution + "n");
promptEnterKey();
break;
case 5:
System.out.println("Thank You for using Paul's Handy Calculator");
System.exit(0);
break;
default:
}
} catch (InputMismatchException ex) {
System.out.println("You have entered an invalid choice. Try again.  ");
String flush =scan.next();
}
} while (choice != 5);
}

您只需要将欢迎消息移出do-white,将初始scan.nextInt()调用移入try块,并将scan.nextInt()调用移出if语句:

// Moved welcome message outside of do-while
System.out.printf("Welcome to Paul's Handy Calculatornn (1) Additionn "
+ "(2) Subtractionn (3) Multiplicationn (4) Divisionn (5) Exitnn");
System.out.printf("What would you like to do? ");
do {
try {
// Moved scan.nextInt inside of try block
choice = scan.nextInt();
if (choice < 1 || choice > 5) {
System.out.printf("You have not entered a number between 1 and 5. " + "Try again.n");
System.out.printf("Enter your choice between 1 and 5 only: n");          
// Removed nextInt call
continue;
}
...

最新更新