亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么java忽略這一行?

為什么java忽略這一行?

蝴蝶不菲 2023-10-13 15:25:34
我正在編寫一個程序來為用戶提供一個菜單。在他輸入數字后,我使用開關來決定用戶應該輸入哪個參數。無論如何,在一種情況下(情況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;


查看完整回答
反對 回復 2023-10-13
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號