我需要接受用户输入并将它们发布到二维数组。这就是我到目前为止所做的:
System.out.print("How many students? ");
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
int col = input;
int row = 5;
String[][] y = new String[col][row];
for (col = 0; col < y.length; col++) {
for(row = 0; row < y[col].length; row++){
int n = col + 1;
System.out.println("Enter the name and grades of student " + n);
y[col][row] = scan.next();
}System.out.println();
}
for(row = 0; row< y.length; row++){
for(col = 0 ;col< y[row].length; col++){
System.out.println(y[row][col]);
}
System.out.println();
}
唯一的问题是,在转到下一个学生之前,它会向同一个问题提出5次问题。我会坚持这条路吗?或者更容易废弃它并采用不同的方法去做它?
答案 0 :(得分:0)
您可以做的是,您可以询问特定学生的所有详细信息,然后继续为下一个学生而不是为每个学生提出相同的问题。
答案 1 :(得分:0)
试试这个:
System.out.print("How many students? ");
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
int col = input;
int row = 5;
String[][] y = new String[col][row];
for (col = 0; col < y.length; col++) {
System.out.println("Enter the name and grades of student " + col);
for(row = 0; row < y[col].length; row++){
int n = col + 1;
y[col][row] = scan.next();
}System.out.println();
}
for(row = 0; row< y.length; row++){
for(col = 0 ;col< y[row].length; col++){
System.out.println(y[row][col]);
}
System.out.println();
}
这将要求每个学生输入一次详细信息。
答案 2 :(得分:0)
如果你想接受姓名和4年级的输入,那么这就是你的 代码,否则详述你的问题
for (col = 0; col < y.length; col++) {
>
> int n = col + 1;
>
> System.out.println("Enter the name and grades of student " + n);
>
> for(row = 0; row < y[col].length; row++){
>
> y[col][row] = scan.next();
> }System.out.println();
> }
答案 3 :(得分:0)
你的这个循环通过每个学生,
for (col = 0; col < y.length; col++)
你的这个循环通过每个音符并要求输入它们,
for(row = 0; row < y[col].length; row++)
因此,您有理由要求用户仅一次输入该笔记,说您每次迭代学生时只需要问 在数组中,只需更改:
for (col = 0; col < y.length; col++) {
for(row = 0; row < y[col].length; row++){
int n = col + 1;
System.out.println("Enter the name and grades of student " + n); //you are asking each time you iterate a note
y[col][row] = scan.next();
}System.out.println();
到此:
for (col = 0; col < y.length; col++) {
System.out.println("Enter the name and grades of student " + col); //you are asking each time you iterate a student
for(row = 0; row < y[col].length; row++){
int n = col + 1;
y[col][row] = scan.next();
}System.out.println();
}