为了账号安全,请及时绑定邮箱和手机立即绑定

为什么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 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信