我正在编写一个程序来为用户提供一个菜单。在他输入数字后,我使用开关来决定用户应该输入哪个参数。无论如何,在一种情况下(情况1)我需要用户的输入。但是在用户输入第一个输入后,程序会中断开关并执行开关之后的操作。代码 :案例一: case 1: System.out.println("Enter the Amount :"); currentAccount.debit(scanner.nextDouble()); System.out.println("Want anything else(yes/no)?"); String input=scanner.nextLine(); if(input.equalsIgnoreCase("no")){ isFinished=true; currentAccount=null; System.out.println("SignedOut successfully"); } break;输出:Choose an opearation: 1.withdraw.2.deposit. 3.transaction history. 1Enter the Amount : 100 Debit amount exceeded account balance. Want anything else(yes/no)? --------- Mhd Bank --------- logined as : -------------------------------- Choose an opearation: 1.withdraw. 2.deposit. 3.transaction history.
1 回答

森林海
TA贡献2011条经验 获得超2个赞
发生这种情况是由于线路未完全消耗Scanner之后的行为。scanner.nextDouble();
尝试这个解决方法
case 1:
System.out.println("Enter the Amount :");
currentAccount.debit(scanner.nextDouble());
scanner.nextLine(); // Consume newline left
System.out.println("Want anything else(yes/no)?");
String input=scanner.nextLine();
if(input.equalsIgnoreCase("no")){
isFinished=true;
currentAccount=null;
System.out.println("SignedOut successfully");
}
break;
添加回答
举报
0/150
提交
取消