我的问题是,从用户收集数据的第一个选项没有连接到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();
}发布于 2014-01-13 06:39:16
以下是我将如何做,修改以适应您自己的规范。
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");
}https://stackoverflow.com/questions/21080950
复制相似问题