我正在使用开关箱制作命令行计算器;这是计算器的主循环:
while(isCalculatorShuttingDown == false) {
tf.println("Type the operation to be conducted: ");
choice = sc.nextLine();
switch(choice.toUpperCase()) {
case "1":
case "ADDITION":
tf.println("Input the first number: ");
input1 = sc.nextInt();
tf.println("Input the second number: ");
input2 = sc.nextInt();
tf.println("The answer is: " + (input1 + input2));
break;
case "5":
case "SHUTDOWN":
isCalculatorShuttingDown = true;
break;
default:
tf.println("Bad Input");
}
}
这很好,但是当循环第二次运行时,它不等待选择变量中的输入,并且由于输入为空,它将进入默认情况,然后再次运行循环,但是这一次它等待输入。为什么?
This is the console:
Type the operation to be conducted:
1
Input the first number:
1
Input the second number:
1
The answer is: 2
Type the operation to be conducted:
Bad Input
Type the operation to be conducted:
您会看到,在“答案是:2”和“键入要执行的操作:”行之后,它不等待用户输入并显示“错误输入”,而是在第三个“键入操作以:进行:“行等待用户输入。该如何解决?