我正在尝试使用 case 语句,并且似乎在某处创建了无限循环。我不知道为什么会发生这种情况。我的代码循环一次,然后而不是再次循环并要求另一个计算选项 - 它不断打印出“无法识别的选项...”
有人能解释一下吗?
这是我的代码。
public static void main(String []args) {
char choice;
boolean exit = false;
UserInterface userMain = new UserInterface();
do {
choice = userMain.menuUI();
switch(choice) {
case 'q':
exit = true;
break;
case 'r':
Real realResult = realCalculation();
System.out.println("\nAnswer = " + realResult.toString() + "\n");
break;
case 'c':
Complex complexResult = complexCalculation();
System.out.println("\nAnswer = " + complexResult.toString() + "\n");
break;
default:
System.out.println("\nUnrecognised option.\n");
break;
}
} while (exit == false);
System.exit(0);
}
最佳答案
这只能是因为 userMain.menuUI();
永远不会返回 q 或 Q
如果您想了解更多详细信息,则必须提供相关代码。
关于Java - 案例循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30398396/