【问题标题】:Switch statement JAVA prompting a new IDswitch语句JAVA提示新ID
【发布时间】:2014-02-23 22:00:01
【问题描述】:

当我通过选择“4:退出”退出菜单后,如何让系统再次提示输入另一个帐户 ID

        Scanner input = new Scanner(System.in);
        System.out.print("Enter an id: ");
        int ID = input.nextInt();


        System.out.println("\nMain Menu ");
        System.out.println("1. Check balance");
        System.out.println("2. Withdraw");
        System.out.println("3. Deposit");
        System.out.println("4. Exit");
        System.out.println("Enter a choice: ");
        int choice = input.nextInt();

    switch (choice ) {  

        case 1: 
        System.out.println("The balance is: " + accountArray[ID].getBalance());
        break;
        case 2: 
        System.out.println("Enter amount to withdraw: ");
        double withdrawAmount = input.nextDouble();
        accountArray[ID].withdraw(withdrawAmount);
        break;
        case 3: 
        System.out.println("Enter amount to deposit: ");
        double depositAmount = input.nextDouble();
        accountArray[ID].deposit(depositAmount);
        break; 
        case 4: 
        System.out.println("Thank you for banking with us");    
        break;
        }

System.out.println("余额为:" + accountArray[ID].getBalance()); }

【问题讨论】:

    标签: java switch-statement case break


    【解决方案1】:

    while(true) 包围所有内容(也许将扫描仪声明放在外面,因为它总是一样的)。

    【讨论】:

      【解决方案2】:

      由于您希望用户能够继续查询其他帐户,请嵌套循环,以便当他们输入“4”时,他们会返回外部循环,提示他们输入新的ID

          Scanner input = new Scanner(System.in);
          int ID;
          while (true) {
            System.out.print("Enter an id: ");
            ID = input.nextInt();
      
            int choice = 0;
            while (chioce != 4) {
              System.out.println("\nMain Menu ");
              System.out.println("1. Check balance");
              System.out.println("2. Withdraw");
              System.out.println("3. Deposit");
              System.out.println("4. Exit");
              System.out.println("Enter a choice: ");
              choice = input.nextInt();
      
              switch (choice ) {  
                 case 1: 
                 System.out.println("The balance is: " + accountArray[ID].getBalance());
                 break;
                 case 2: 
                 System.out.println("Enter amount to withdraw: ");
                 double withdrawAmount = input.nextDouble();
                 accountArray[ID].withdraw(withdrawAmount);
                 break;
                 case 3: 
                 System.out.println("Enter amount to deposit: ");
                 double depositAmount = input.nextDouble();
                 accountArray[ID].deposit(depositAmount);
                 break; 
                 case 4: 
                 System.out.println("Thank you for banking with us");    
                 break;
              }
      
              System.out.println("The Balance is: " + accountArray[ID].getBalance());
            }
          }
      

      如果您希望用户能够终止程序,请将外部循环条件从 true 更改为类似于内部循环的内容并相应地提示它们,即“输入 id 或 -1 退出”。

      【讨论】:

        【解决方案3】:

        您可以使用while 循环:

        int choice = 0;
        
        while (choice != 4) {
            System.out.print("Enter an id: ");
            int ID = input.nextInt();
            ...
            int choice = input.nextInt();
            ...
        }
        

        编辑:

        int choice = 0;
        
        while (true) {
            System.out.print("Enter an id: ");
            int ID = input.nextInt();
        
            while(choice != 4) {
                ...
                int choice = input.nextInt();
                ...
            }
        }
        

        【讨论】:

        • 我需要能够继续从主菜单中选择选项,直到我选择 4。相反,我只能选择一个选项并且系统要求我提供新的帐户 ID。请帮忙
        • 然后使用类似的嵌套while 循环,但包含显示选项的部分和choice。您必须在外循环中使用while(true)。见编辑。
        猜你喜欢
        • 1970-01-01
        • 2015-10-18
        • 2016-04-16
        • 2011-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多