如何将平均考试成绩组织成文件中的列?
How to get average of test scores organized into columns in a file?
该文件包含以下格式的数据:
Name|Test1|Test2|Test3|Test4|Test5|Test6|Test7|Test8|Test9|Test10
John Smith|82|89|90|78|89|96|75|88|90|96
Jane Doe|90|92|93|90|89|84|97|91|87|91
Joseph Cruz|68|74|78|81|79|86|80|81|82|87
Suzanne Nguyen|79|83|85|89|81|79|86|92|87|88
我正在尝试找出如何获得每列的总和(例如测试 1 = 82 + 92 + 68 + ...)以最终计算每个测试的平均分数。
这是我解析文件并进行其他计算的方式:
public class TestAverages
{
private static int[] grades;
private static int[] testTotal;
private static int N;
private static double classTotal;
public static void main(String[] args) throws FileNotFoundException
{
File input = new File("TestData.txt");
Scanner in = new Scanner(input);
parseFile(in);
}
public static void parseFile(Scanner in) throws FileNotFoundException
{
TestAverages t = new TestAverages();
in.nextLine(); //skips the first line of text in file (labels)
double classAvg =0.0;
while(in.hasNextLine())
{
String line = in.nextLine();
String[] data = line.split("\|");
String name = data[0];
grades = new int[data.length - 1];
N = grades.length;
for(int i = 0; i < N; i++)
{
grades[i] = Integer.parseInt(data[i + 1]);
}
System.out.println(name);
System.out.println("Student Average: " + t.getStudentAvg(grades) + "%\n");
classAvg = t.getClassAvg(grades);
System.out.println("Test Average: " + t.getTestAvg(grades) + "%\n");
}
System.out.printf("\nClass Average: %.2f%%\n", classAvg );
}
public double getStudentAvg(int[] grades)
{
double total = 0.0;
double avg = 0.0;
int N = grades.length;
for(int i = 0; i < N; i++){
total += grades[i];}
avg = total / N;
return avg;
}
public double getClassAvg(int[] grades)
{
double classTotal = getStudentAvg(grades) / N;
double classAvg =0.0;
classTotal += classTotal;
classAvg = classTotal;
return classTotal;
}
}
格式不达标请见谅
我目前的主要问题是如何为每个学生提取每个测试的分数并将所有内容相加。
如果你已经知道文件中有 10 个测试总数,那应该很容易。
你可以让 int[] testTotal = new int[10];
代替 testTotal[0] = sum-of-first-column
, testTotal[1] = sum-of-second-column
等等。 . .
你可以读入每一行并用 "\|"
作为正则表达式拆分它们
当你在线路中循环时,让testTotal[0] += data[1]
,testTotal[1] += data[2]
。 . . testTotal[9] += data[10]
因为 data[0]
是学生的名字。
正在计算每个学生的考试成绩。您需要的是 class 平均值和测试平均值。
- 考试平均分是每次考试的平均分。
- 学生平均分是每个学生的平均考试成绩
- class 平均值是总体 class 在所有测试中的表现。
考虑三个测试
test1 test2 test3 student average
student1 90 80 70 240/3
student2 100 90 90 280/3
student3 80 80 90 250/3
testAvg 270/3 250/3 250/3
class avg = (270 + 250 + 250)/9 or (240+280+250)/9
所以你需要以这样的方式读入值,以便于进行其他计算。我建议对数据使用 Map<String,List<Integer>>
,其中字符串是学生姓名,列表是每个学生的考试成绩。或者您可以使用 2D int
数组。但是您需要保存分数,以便可以对测试分数进行列平均。大部分工作已完成。
另外,我注意到两个问题。您调用了方法 getTestAvg
但未声明它,您声明了方法 getClassAvg
但从未调用它。
该文件包含以下格式的数据:
Name|Test1|Test2|Test3|Test4|Test5|Test6|Test7|Test8|Test9|Test10
John Smith|82|89|90|78|89|96|75|88|90|96
Jane Doe|90|92|93|90|89|84|97|91|87|91
Joseph Cruz|68|74|78|81|79|86|80|81|82|87
Suzanne Nguyen|79|83|85|89|81|79|86|92|87|88
我正在尝试找出如何获得每列的总和(例如测试 1 = 82 + 92 + 68 + ...)以最终计算每个测试的平均分数。
这是我解析文件并进行其他计算的方式:
public class TestAverages
{
private static int[] grades;
private static int[] testTotal;
private static int N;
private static double classTotal;
public static void main(String[] args) throws FileNotFoundException
{
File input = new File("TestData.txt");
Scanner in = new Scanner(input);
parseFile(in);
}
public static void parseFile(Scanner in) throws FileNotFoundException
{
TestAverages t = new TestAverages();
in.nextLine(); //skips the first line of text in file (labels)
double classAvg =0.0;
while(in.hasNextLine())
{
String line = in.nextLine();
String[] data = line.split("\|");
String name = data[0];
grades = new int[data.length - 1];
N = grades.length;
for(int i = 0; i < N; i++)
{
grades[i] = Integer.parseInt(data[i + 1]);
}
System.out.println(name);
System.out.println("Student Average: " + t.getStudentAvg(grades) + "%\n");
classAvg = t.getClassAvg(grades);
System.out.println("Test Average: " + t.getTestAvg(grades) + "%\n");
}
System.out.printf("\nClass Average: %.2f%%\n", classAvg );
}
public double getStudentAvg(int[] grades)
{
double total = 0.0;
double avg = 0.0;
int N = grades.length;
for(int i = 0; i < N; i++){
total += grades[i];}
avg = total / N;
return avg;
}
public double getClassAvg(int[] grades)
{
double classTotal = getStudentAvg(grades) / N;
double classAvg =0.0;
classTotal += classTotal;
classAvg = classTotal;
return classTotal;
}
}
格式不达标请见谅
我目前的主要问题是如何为每个学生提取每个测试的分数并将所有内容相加。
如果你已经知道文件中有 10 个测试总数,那应该很容易。
你可以让 int[] testTotal = new int[10];
代替 testTotal[0] = sum-of-first-column
, testTotal[1] = sum-of-second-column
等等。 . .
你可以读入每一行并用 "\|"
作为正则表达式拆分它们
当你在线路中循环时,让testTotal[0] += data[1]
,testTotal[1] += data[2]
。 . . testTotal[9] += data[10]
因为 data[0]
是学生的名字。
正在计算每个学生的考试成绩。您需要的是 class 平均值和测试平均值。
- 考试平均分是每次考试的平均分。
- 学生平均分是每个学生的平均考试成绩
- class 平均值是总体 class 在所有测试中的表现。
考虑三个测试
test1 test2 test3 student average
student1 90 80 70 240/3
student2 100 90 90 280/3
student3 80 80 90 250/3
testAvg 270/3 250/3 250/3
class avg = (270 + 250 + 250)/9 or (240+280+250)/9
所以你需要以这样的方式读入值,以便于进行其他计算。我建议对数据使用 Map<String,List<Integer>>
,其中字符串是学生姓名,列表是每个学生的考试成绩。或者您可以使用 2D int
数组。但是您需要保存分数,以便可以对测试分数进行列平均。大部分工作已完成。
另外,我注意到两个问题。您调用了方法 getTestAvg
但未声明它,您声明了方法 getClassAvg
但从未调用它。