如何使程序能够处理用户输入的其他符号?一会儿/尝试赶上?(java)

亨里克

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

用户可以通过按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!");
    }
  }
}
踢巴托夫斯基

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

公共布尔hasNextInt()

如果可以使用nextInt()方法将此扫描器输入中下一个标记解释为默认基数中的int值,则返回true扫描仪不会前进超过任何输入。返回:如果且仅当此扫描器的下一个标记是有效的int值时,才返回true抛出: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");
        }
    }

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章