Java—第一次运行do while循环后重复输出
我正在尝试为学校作业创建一个小型/简单的菜单驱动的银行应用程序。我正在运行do while循环,询问用户希望使用哪个命令。在获得用户输入并运行一次循环后,第二次显示命令列表时,它会打印两次Java—第一次运行do while循环后重复输出,java,loops,output,do-while,repeat,Java,Loops,Output,Do While,Repeat,我正在尝试为学校作业创建一个小型/简单的菜单驱动的银行应用程序。我正在运行do while循环,询问用户希望使用哪个命令。在获得用户输入并运行一次循环后,第二次显示命令列表时,它会打印两次 do { System.out.println("Here is a list of possible commands:"); System.out.println("Create Customer:'c'"); System.out.println("Exi
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'
非常感谢你的帮助 添加read=keyboard.nextLine();在你的后面: System.out.println(“\n谢谢!已创建新客户!\n”)
如果您指向的是不等于“x”或“c”的对象,则必须将指针移到空行,否则您的程序将不会等待您的输入。非常感谢!已经修好了。我真的很感谢你的帮助。
read = keyboard.nextLine();
System.out.println("\nThank you! A new customer has been created!\n");