我正在編寫一個程序來為用戶提供一個菜單。在他輸入數(shù)字后,我使用開關(guān)來決定用戶應(yīng)該輸入哪個參數(shù)。無論如何,在一種情況下(情況1)我需要用戶的輸入。但是在用戶輸入第一個輸入后,程序會中斷開關(guān)并執(zhí)行開關(guān)之后的操作。代碼 :案例一: 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條經(jīng)驗 獲得超2個贊
發(fā)生這種情況是由于線路未完全消耗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
提交
取消