找不到字段匹配字段

时间:2014-01-12 22:11:28

标签: java if-statement try-catch

我的问题是从用户收集数据的第一个选项没有连接到if语句中的第二个选项(都是粗体)。有没有人对此有任何建议?

Scanner keyboard = new Scanner(System.in);

System.out.println("Please select from the following options:");
System.out.println("1. Enter new Policy");
System.out.println("2. Display summary of policies");
System.out.println("3. Display summary of policies for selected month");
System.out.println("4. Find and display Policy");
System.out.println("0. Exit");

try {
    **int option** = Integer.parseInt(keyboard.nextLine());
} catch (NumberFormatException e) {
    System.out.println("Error : program closing");
    System.exit(0);
}

if **(option == 1)** {
    System.out.println("Please enter full name: ");
    String name = keyboard.nextLine();

    while (name.length() >= 21) {
        System.out.print("Name is invalid, please re enter:");
        name = keyboard.nextLine();
    }

1 个答案:

答案 0 :(得分:0)

以下是我将如何操作,修改以符合您自己的规格。

public static void Options_() {

   select_option();// Here you invoking the list of options available 

       Scanner keyboard = new Scanner(System.in);
       int number = keyboard.nextInt();

    if (number == 1) {
         //Do something here        

   } else 

        if (number == 2) {
        // Do Something Else        

  }
       //and So on
  }
      public static void select_option() {
      System.out.println("Please select from the following options:");
      System.out.println("1. Enter new Policy");
      System.out.println("2. Display summary of policies");
      System.out.println("3. Display summary of policies for selected month");
      System.out.println("4. Find and display Policy");
      System.out.println("0. Exit");
  }