繁体   English   中英

在java中同时输入多个String数据

[英]enter multiple String data in java at the same time

我有以下代码:

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类实例。

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

对不起,我不明白你的意思......我试过这个,我可以插入两个值(iusername和ipassword):

import java.util.Scanner;

public class MultipleStringInput {

   public static void main(String[] args) {
      String username = "administrator", iusername = "", password = "password", ipassword = "";
      System.out.println("Please login to the system");
      System.out.print("\nusername:\t");

      Scanner scan = new Scanner(System.in);

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

}

我认为这段代码按预期工作(“我希望用户输入两个变量的数据”)。 看起来你的IDE正在将System.out重定向到你的扫描仪......奇怪......我建议你从命令行测试它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM