【发布时间】:2016-12-13 14:48:43
【问题描述】:
我有以下程序,在运行它时,我打印了 3 次菜单。我只期待一个并立即提示输入。
class whileexample2
{
public static void main(String args[]) throws java.io.IOException
{
char i;
int a = 100;
int b = 20;
do
{
System.out.println("select your choice");
System.out.println("---------------------");
System.out.println("(1) Additon");
System.out.println("(2) Subtraction");
System.out.println("(3) Multiplication");
System.out.println("(4) Division");
i = (char) System.in.read();
} while (i < '1' || i > '4');
System.out.println("\n");
switch(i)
{
case '1':
{
System.out.println("Result of addition is: " + (a + b));
break;
}
case '2':
{
System.out.println("Result of subtraction is: " + (a - b));
break;
}
case '3':
{
System.out.println("Result of multiplication is: " + (a * b));
break;
}
case '4':
{
System.out.println("Result of division is: " + (a / b));
break;
}
}
}
}
输出
选择您的选择
---------------------
(1) 附加
(2) 减法
(3) 乘法
(4) 师
7 //输入错误,因此再次显示菜单,但会打印3次 选择您的选择
---------------------
(1) 附加
(2) 减法
(3) 乘法
(4) 师
选择您的选择
---------------------
(1) 附加
(2) 减法
(3) 乘法
(4) 师
选择您的选择
---------------------
(1) 附加
(2) 减法
(3) 乘法
(4) 师
2减法结果为:80
【问题讨论】:
-
是不是因为你在 do...while 循环中打印了菜单?
-
@BretC:我想在输入无效输入时重复菜单,因此我在 do..while 循环中打印它。但不知道为什么它在无效输入输入时显示 3 次。输入正确的输入时,它可以正常工作。
标签: java