Java IDE先打印两行,然后才能解析用户';s输入
所以我现在很难解决这个问题。简而言之,我正在尝试为一个类创建一个银行应用程序。我正在写一个方法来打印并获取用户的输入,但是打印行在我获取输入之前会打印出这两行。这是我的实现Java IDE先打印两行,然后才能解析用户';s输入,java,java.util.scanner,Java,Java.util.scanner,所以我现在很难解决这个问题。简而言之,我正在尝试为一个类创建一个银行应用程序。我正在写一个方法来打印并获取用户的输入,但是打印行在我获取输入之前会打印出这两行。这是我的实现 boolean done = false; while(!done) { System.out.println("Welcome to Running Bank" + "\nWhat would you like to do?"
boolean done = false;
while(!done) {
System.out.println("Welcome to Running Bank"
+ "\nWhat would you like to do?"
+ "\n1-Create Account"
+ "\n2-Log-In"
+ "\n3-Exit");
choice = scanner.nextInt();
switch(choice) {
case 1:
System.out.println("Input your username(No more than 15 characters: ");
Username = scanner.nextLine();
System.out.println("Input your password(Must be at least 8 characters and not over 24): ");
Password = scanner.nextLine();
}
}
}
我期望它能做的是打印出“输入你的用户名”,然后在我得到用户的输入后再打印下一行。然而,现在发生的事情是,在我输入任何内容之前,两行都被打印出来了。
提前谢谢你
boolean done = false;
while(!done) {
System.out.println("Welcome to Running Bank"
+ "\nWhat would you like to do?"
+ "\n1-Create Account"
+ "\n2-Log-In"
+ "\n3-Exit");
choice = scanner.nextInt();
scanner.nextLine();
switch(choice) {
case 1:
System.out.println("Input your username(No more than 15 characters: ");
Username = scanner.nextLine();
System.out.println("Input your password(Must be at least 8 characters and not over 24): ");
Password = scanner.nextLine();
}
}
}
现在应该可以了。在输入choice
之后,我添加了一个scanner.nextLine()
,choice=scanner.nextLine()
(正如Johannes Kuhn在上面的评论中所说的那样)可能重复的Putscanner.nextLine()