【问题标题】:How do I make my program able to handle other symbols input by users? while/try-catch? (java)如何使我的程序能够处理用户输入的其他符号? while/try-catch? (爪哇)
【发布时间】:2014-10-12 18:53:07
【问题描述】:

我是编程新手,目前正在为一家银行编写菜单。

用户可以通过按 1 或 2 来选择他/她是管理员还是客户。我想编写代码,以便如果用户键入除 ints 之外的其他符号,程序将发送错误消息并让用户再次选择。

到目前为止,我只设法让程序使用while 循环来处理除 1 和 2 之外的其他整数。

我想我应该使用trycatch,但我无法让它工作。我用//----------标记了我尝试过try/catch的地方。

用户输入 X 而不是数字时的错误消息:

run:

Press 1 to login as customer or 2 to login as admin x
Exception in thread "main" java.util.InputMismatchException
  at java.util.Scanner.throwFor(Scanner.java:864)
  at java.util.Scanner.next(Scanner.java:1485)
  at java.util.Scanner.nextInt(Scanner.java:2117)
  at java.util.Scanner.nextInt(Scanner.java:2076)
  at bank.Bank.main(Bank.java:32)
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)

public class Bank {
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int ChoiceOne;

    int CustpNr;
    int CustChoice;

    int AdminpNr;
    int AdminChoice;

    System.out.print("Press 1 to login as customer or 2 to login as admin ");
    ChoiceOne = input.nextInt();

    while (ChoiceOne != 1 && ChoiceOne != 2) {
      // ---------------

      try {
        ChoiceOne = input.nextInt();
      } catch (Exception e) {
        continue;
      }

      // ----------------

      System.out.print(" Wrong number. Press 1 to login as customer or 2 to login as admin ");
      ChoiceOne = input.nextInt();
    }// ends while

    // The code below generates a menu for the customer if the user chooses 1
    // and a meny for the admin if the user chooses 2.

    if (ChoiceOne == 1) {
      System.out.print("Welcome customer. Please login by using your birthdate (yymmdd) ");
      CustpNr = input.nextInt();

      boolean quit = false;
      do {
        System.out.println("1. deposit money");
        System.out.println("2. Withdraw money");
        System.out.println("3. Check balance");
        System.out.print("Your choice, 0 to quit: ");
        CustChoice = input.nextInt();

        switch (CustChoice) {
        case 1:
          // deposit money
          break;
        case 2:
          // withdraw money
          break;
        case 3:
          // Check balance and accounts
          break;
        case 0:
          quit = true;
          break;
        default:
          System.out.println("Wrong choice.");
          break;
        }

        System.out.println();
      } while (!quit);

      System.out.println("Bye!");
    } else if (ChoiceOne == 2) {
      System.out.print("Welcome Admin. Please login using your birthdate (yymmdd) ");
      AdminpNr = input.nextInt();

      boolean quit = false;
      do {
        System.out.println("1. Add customer");
        System.out.println("2. Add account");
        System.out.println("3. List customer and accounts");
        System.out.println("4. Remove customer");
        System.out.println("5. Remove account");
        System.out.print("Your choice, 0 to quit: ");
        AdminChoice = input.nextInt();

        switch (AdminChoice) {
        case 1:
          // add customer
          break;
        case 2:
          // add account
          break;
        case 3:
          // List customer and accounts
          break;
        case 4:
          // ta bort kund
          break;
        case 5:
          // ta bort konto
          break;
        case 0:
          quit = true;
          break;
        default:
          System.out.println("Wrong choice.");
          break;
        }

        System.out.println();
      } while (!quit);

      System.out.println("Bye!");
    }
  }
}

【问题讨论】:

  • 看起来不错 - 有什么问题?
  • @Henrik 我发布了我的答案让我知道它是否对你有帮助:)
  • 对不起,当我第一次发布问题时,我没有在运行程序时添加错误消息。如果我运行程序并输入“x”或其他字符,它会崩溃。
  • @Henrik 你解决了你的问题吗?
  • @Kick Buttowski 抱歉我的回答迟了。我尝试使用蓝图几个小时,但我不明白我做错了什么,然后我的大脑崩溃了:P.. 无论如何。我现在又试了一次并添加了'input.next();'在“其他”之后,现在它可以工作了。非常感谢 =)

标签: java bank


【解决方案1】:

您必须使用hasNextInt() 来检查输入的数字是否为整数类型。如果您输入任何其他类型,您将获得java.util.InputMismatchException

公共布尔 hasNextInt()

返回 true 如果 下一个标记在此 扫描仪的输入在默认情况下可以解释为一个 int 值 基数使用 nextInt() 方法。扫描仪不前进 任何输入。返回:当且仅当此扫描器的下一个标记是 有效的 int 值抛出:IllegalStateException - 如果此扫描器是 关闭

您可以使用以下示例作为您的蓝图

例如:

      int i = 0;
      while (i == 0) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter just int");
        if (input.hasNextInt()) {
            System.out.println(input.nextInt());
            System.out.println("good day");
            i = 1;
        } else {
            System.out.println("please enter type int");
        }
    }

【讨论】:

  • 非常感谢!这行得通。我必须添加 input.next();之后。
  • 为什么需要 input.next()?
【解决方案2】:

问题是你在两个地方使用了ChoiceOne=input.nextInt()try...catch 仅覆盖循环内的那个。

按照其他答案中的建议,删除循环之前的调用,并使用带中断的循环。您可以在致电nextInt() 之前提前查看,也可以拨打java.util.InputMismatchException

【讨论】:

    【解决方案3】:

    扫描仪输入.nextInt();在 System.in 上使用时将强制用户输入一个 int,并返回该值。

    您不必捕获它的异常,这意味着仅具有以下代码就足以强制您的值为 1 或 2:

    ChoiceOne=input.nextInt();
    
    while (ChoiceOne != 1 && ChoiceOne !=2 )
         ChoiceOne=input.nextInt();
    

    【讨论】:

    • 嗯,好的。抱歉,我第一次发布时没有添加错误消息。我尝试了您的建议,但如果用户输入“X”,程序就会崩溃,并且我会收到我现在添加到问题中的错误消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 2022-10-16
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多