我正在尝试为学校作业创建一个小/简单菜单驱动的银行应用程序。我正在运行一个do-while循环来询问用户想要使用哪个命令。在我获得用户输入并且循环运行一次后,第二次显示命令列表时,它会打印两次。
do {
System.out.println("Here is a list of possible commands:");
System.out.println("Create Customer:'c'");
System.out.println("Exit Application:'x'");
read = keyboard.nextLine();
if (read.equalsIgnoreCase("c")) {
System.out.println("You have chosen to create a new customer!");
System.out.println("\nPlease enter the customers first name: ");
firstName = keyboard.nextLine();
System.out.println("\nPlease enter the customers last name:");
lastName = keyboard.nextLine();
System.out.println("\nPlease enter the customers address: ");
address = keyboard.nextLine();
System.out.println("\nPlease enter the customers email: ");
email = keyboard.nextLine();
System.out.println("\nPlease enter the customers phone number: ");
phoneNum = keyboard.nextLine();
System.out.println("\nPlease enter the customers age: ");
age = keyboard.nextInt();
System.out.println("\nPlease enter the customers initial deposit: ");
deposit = keyboard.nextDouble();
Customer tempCust = new Customer(firstName, lastName, address, email, age, phoneNum);
tempCust.createAccount(deposit);
customers.add(tempCust);
System.out.println("\nThank you! A new customer has been created!\n");
}
} while (!read.equalsIgnoreCase("x"));
第一次循环运行时会显示:
以下是可能的命令列表: 创建客户:' c' 退出申请:' x'
但第二次运行它会显示:
以下是可能的命令列表: 创建客户:' c' 退出申请:' x' 以下是可能的命令列表: 创建客户:' c' 退出申请:' x'
非常感谢您的帮助!
答案 0 :(得分:0)
添加read = keyboard.nextLine();在你之后: System.out.println(“\ n谢谢!创建了一个新客户!\ n”);
read = keyboard.nextLine();
System.out.println("\nThank you! A new customer has been created!\n");
你指的是不等于'x'或'c'的东西,你必须将指针移动到空行,否则你的程序将不会等待你的输入。