[英]First element of array is al;ways null - java
Whenever i put the first students name in when it displays them it always comes up as null however nothing else does this.Why? 每当我在显示学生的名字时输入第一个学生的名字时,它总是出现为null,但这没有其他作用。为什么?
package school.register;
import java.util.Scanner;
public class SchoolRegister {
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
String[][] Students = new String[5][2];
int i = 0;
int j = 0;
for (i = 0; i < 5; i++) {
System.out.println("Please put in a students name.");
Students[i][j] = Input.nextLine();
for (j = 0; j < 1; j++) {
System.out.println("Please put in a students test score.");
Students[i][j] = Input.nextLine();
}
}
System.out.println("Score\tName");
display(Students);
}
public static void display(String x[][]) {
for (int row = 0; row < x.length; row++) {
for (int column = 0; column < x[row].length; column++) {
System.out.println(x[row][column]);
}
}
}
}
try this: 尝试这个:
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
String[][] Students = new String[5][2];
int i;
int l = 0;
for (i = 0; i < 5; i++) {
System.out.println("Please put in a students name.");
Students[i][0] = Input.nextLine();
System.out.println("Please put in a students test score.");
Students[i][1] = Input.nextLine();
}
System.out.println("Score\tName");
Display(Students);
}
System.out.println("Please put in a students name.");
Students[i][0] = Input.nextLine();
System.out.println("Please put in a students test score.");
Students[i][1] = Input.nextLine();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.