打印二维数组时遇到问题
Having problems printing a 2D array
编写一个程序,从用户提供的文件中读取学生的姓名及其考试成绩。
文件中的前两个值表示学生人数,后面是测试次数。然后程序应计算每个学生的平均考试成绩并分配适当的分数 (A, B, C, D, E or F
) 以及每个考试的平均分。
外部文件
tom 91 67 84 50 69
suzy 74 78 58 62 64
Peter 55 95 81 77 61
Paul 91 95 92 77 86
Diane 91 54 52 53 92
Emily 82 71 66 68 95
Natalie 97 76 71 88 69
Ben 62 67 99 85 94
Mark 53 61 72 83 73
Anna 64 91 61 53 68
根据我老师给我们的例子,我一直在尝试让它打印二维数组中的列表。
private static final String FILENAME = "/Users/Kal-El/Documents/Programming/grades.txt";
public static void main(String[] args) {
//below code to read from a file
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
String students [] = new String[10];
int [][] grades = new int [10][5];
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
students = sCurrentLine.split(" ");
for (int i=0; i < students.length; i++){
for (int j=0; j < grades.length; j++) {
System.out.print(grades[i][j]+" ");
} // end of inner for loop
} //end of outer for loop
} // end of while loop
} catch (IOException e) {
e.printStackTrace();
} // end of catch
}
调用 currentLine.split(" ")
将 return 整行由空格分隔。所以行
tom 91 67 84 50 69
会变成这个数组:
["tom", "91", "67", "84", "50", "69"]
所以像这样循环
for (int i=0; i < students.length; i++)
不是遍历所有学生,而是遍历一个学生的名字和他们的所有分数。数组中的第一项将是学生的姓名,因此您可以通过执行 System.out.print(students[0])
.
来输出它
您可以通过执行以下操作循环查看该学生的分数:
for (int i = 1; i < students.length; i++)
边做边总结它们以获得平均值。我不确定您对 grades
数组数组的概念是什么,但我认为您不需要它。
编写一个程序,从用户提供的文件中读取学生的姓名及其考试成绩。
文件中的前两个值表示学生人数,后面是测试次数。然后程序应计算每个学生的平均考试成绩并分配适当的分数 (A, B, C, D, E or F
) 以及每个考试的平均分。
外部文件
tom 91 67 84 50 69
suzy 74 78 58 62 64
Peter 55 95 81 77 61
Paul 91 95 92 77 86
Diane 91 54 52 53 92
Emily 82 71 66 68 95
Natalie 97 76 71 88 69
Ben 62 67 99 85 94
Mark 53 61 72 83 73
Anna 64 91 61 53 68
根据我老师给我们的例子,我一直在尝试让它打印二维数组中的列表。
private static final String FILENAME = "/Users/Kal-El/Documents/Programming/grades.txt";
public static void main(String[] args) {
//below code to read from a file
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
String students [] = new String[10];
int [][] grades = new int [10][5];
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
students = sCurrentLine.split(" ");
for (int i=0; i < students.length; i++){
for (int j=0; j < grades.length; j++) {
System.out.print(grades[i][j]+" ");
} // end of inner for loop
} //end of outer for loop
} // end of while loop
} catch (IOException e) {
e.printStackTrace();
} // end of catch
}
调用 currentLine.split(" ")
将 return 整行由空格分隔。所以行
tom 91 67 84 50 69
会变成这个数组:
["tom", "91", "67", "84", "50", "69"]
所以像这样循环
for (int i=0; i < students.length; i++)
不是遍历所有学生,而是遍历一个学生的名字和他们的所有分数。数组中的第一项将是学生的姓名,因此您可以通过执行 System.out.print(students[0])
.
您可以通过执行以下操作循环查看该学生的分数:
for (int i = 1; i < students.length; i++)
边做边总结它们以获得平均值。我不确定您对 grades
数组数组的概念是什么,但我认为您不需要它。