所以,我是一名初级编码员,我正在开设一个课程,打印一个基本的2D数组,其数据类似于学校级程序。
public class Skyward {
public void Student(){
String [][] studentArray=new String[3][4];
String lastName1="Brown";
String lastName2="Johnson";
String lastName3="Michaelson";
//variables for LastName
String name1="Bob";
String name2="Jenny";
String name3="Michael";
//variables for FirstName
String grade1="11";
String grade2="10";
String grade3="12";
//variables for Grade
String t1grade1="A";
String t1grade2="C";
String t1grade3="F";
//variables for T1Grade
studentArray[0][0]=lastName1;
studentArray[0][1]=name1;
studentArray[0][2]=grade1;
studentArray[0][3]=t1grade1;
studentArray[1][0]=lastName2;
studentArray[1][1]=name2;
studentArray[1][2]=grade2;
studentArray[1][3]=t1grade2;
studentArray[2][0]=lastName3;
studentArray[2][1]=name3;
studentArray[2][2]=grade3;
studentArray[2][3]=t1grade3;
for ( int row=0; row < studentArray.length; row++ ) {
for ( int col=0; col < studentArray[row].length; col++ ){
System.out.print(studentArray[row][col]);
System.out.print(", ");
System.out.print(" -Grade- ");
System.out.print(" T1Grade- ");
}//end inner loop
System.out.println("");
}//end outer loop
}//end method
}//end class
我需要打印为&#34; LastName,FirstName-Grade-x-T1Grade-y&#34;,而是它像这样出来
Brown, -Grade- T1Grade- Bob, -Grade- T1Grade- 11, -Grade- T1Grade- A, -Grade- T1Grade-
我不确定我做错了什么,主要是在标点符号和循环中。我尝试在这个网站上查看其他2D阵列打印问题,但由于我在编码方面仍然相当新,所以大多数其他问题或他们的答案都在我脑海中。任何提示都将非常感谢!
答案 0 :(得分:0)
不需要内循环。它迭代studentArray
中的列数,因此它会在每次迭代时调用所有System.out.prints
。您可以改为使用以下内容:
for ( int row=0; row < studentArray.length; row++ ) {
System.out.print(studentArray[row][0])
System.out.print(", " + studentArray[row][1]);
System.out.print(" -Grade- " + studentArray[row][2]);
System.out.print(" T1Grade- " + studentArray[row][3]);
System.out.println("");
}//end loop