我有以下代码:

String username="administrator", iusername="", password="password", ipassword="";
System.out.println("Please login to the system");
System.out.print("\nusername:\t");

iusername = scan.nextLine();
System.out.print("\npassword:\t");
ipassword = scan.nextLine();


我希望用户输入两个变量(iusername和ipassword)的数据
但我只能输入ipassword的数据,因为它会跳过

iusername = scan.nextLine();


以下是IDE输出的示例

username:   username:

password:   BUILD STOPPED (total time: 12 seconds)


它跳过我的用户名并输入密码

最佳答案

这可用于将新的输出自动添加到新行中。

System.out.println("");


没有更多的代码,或更清晰的问题,这很难回答。

username = scan.nextLine();
System.out.print("\npassword:\t");
password = scan2.nextLine();


应该如此,因为您可以重用Scanner类的Instance。

username = scan.nextLine();
System.out.print("\npassword:\t");
password = scan.nextLine();


但是也许你想做

username = scan.nextLine(); // Get user input
System.out.println("Username:\t" + username); // Show typed username
password = scan2.nextLine(); // Get user input again
System.out.println("Password:\t" + password); // Show typed password

10-07 19:07
查看更多