gpt4 book ai didi

java - 在我可以解析用户的输入之前,IDE 会打印这两行

转载 作者:行者123 更新时间:2023-12-01 20:18:48 25 4
gpt4 key购买 nike

所以我目前很难解决这个问题。简而言之,我正在尝试为类(class)创建一个银行应用程序。我正在编写一种方法来打印并获取用户的输入,但打印行会在我获取输入之前打印出两行。这是我的实现。

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()

关于java - 在我可以解析用户的输入之前,IDE 会打印这两行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58946485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com