Java 我正在2D数组中存储字符串。但是,当我打印结果时,[0][0]空间是空白的。有人能告诉我怎么占据那个空间吗?

Java 我正在2D数组中存储字符串。但是,当我打印结果时,[0][0]空间是空白的。有人能告诉我怎么占据那个空间吗?,java,string,multidimensional-array,Java,String,Multidimensional Array,如何使用用户提供的信息占据[0][0]空间。打印时,它从[0][1]开始,而不是从[0][0] Scanner scan = new Scanner(System.in); int sizeArray; //Prompting user for number of users to be added System.out.println("How many users are you going to add?"); sizeArray = scan.nextInt(); //Reading

如何使用用户提供的信息占据[0][0]空间。打印时,它从[0][1]开始,而不是从[0][0]

Scanner scan = new Scanner(System.in);
int sizeArray;

//Prompting user for number of users to be added
System.out.println("How many users are you going to add?");
sizeArray = scan.nextInt(); //Reading users to add

String user [][] = new String[sizeArray][2];


System.out.println("Enter name followed by unique password: ");

for(int i = 0; i < sizeArray; i++) {
    for (int j = 0; j< 2; j++) {
        user[i][j] =scan.nextLine();
    }
}
System.out.println("You entered: ");

for(int i = 0; i < sizeArray; i++) {
    for(int j = 0; j < 2; j++) {
        System.out.println(" Name["+i+"]["+j+"] = "+user[i][j]);
    }
    System.out.print("");
}
Scanner scan=新的扫描仪(System.in);
int sizeArray;
//提示用户输入要添加的用户数
System.out.println(“您要添加多少用户?”);
sizerray=scan.nextInt()//正在阅读要添加的用户
字符串用户[][]=新字符串[sizeArray][2];
System.out.println(“输入名称,后跟唯一密码:”);
对于(int i=0;i
打印时,它从[0][1]开始,而不是从[0][0]

Scanner scan = new Scanner(System.in);
int sizeArray;

//Prompting user for number of users to be added
System.out.println("How many users are you going to add?");
sizeArray = scan.nextInt(); //Reading users to add

String user [][] = new String[sizeArray][2];


System.out.println("Enter name followed by unique password: ");

for(int i = 0; i < sizeArray; i++) {
    for (int j = 0; j< 2; j++) {
        user[i][j] =scan.nextLine();
    }
}
System.out.println("You entered: ");

for(int i = 0; i < sizeArray; i++) {
    for(int j = 0; j < 2; j++) {
        System.out.println(" Name["+i+"]["+j+"] = "+user[i][j]);
    }
    System.out.print("");
}
您在
scan.nextLine()
之前调用
scan.nextLine()
方法,该方法不使用用户输入的最后一个换行符,因此循环中的第一个
scan.nextLine()
将使用该换行符,并且当您尝试在
[0][0]检索元素时
用户
二维数组中,它将打印一个空的
字符串

下面是一个快速解决方案,可让您打印从
[0][0]
开始的期望值:

System.out.println("Enter name followed by unique password: ");
插入以下内容:

scan.nextLine();
现在它变成了:

System.out.println("Enter name followed by unique password: ");
scan.nextLine(); // let this consume the newline character
publicstaticvoidmain(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
int sizeArray;
System.out.println(“您要添加多少用户?”);
sizeArray=Integer.parseInt(scan.nextLine());
字符串用户[][]=新字符串[sizeArray][2];
System.out.println(“输入名称,后跟唯一密码:”);
对于(int i=0;i

我想这就是你问题的答案吧?:)

问题是,您想让您的方法从索引[0][1]打印吗?
sizerray=Integer.parseInt(scan.nextLine())可能重复非常感谢!!我不敢相信事情会这么简单。@cbc很高兴它能帮上忙。如果答案解决了你的问题,别忘了标记为已接受。