如何计算文件中按列组织的测试分数的平均值?

huangapple go评论99阅读模式
英文:

How to get average of test scores organized into columns in a file?

问题

以下是翻译好的代码部分:

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(); // 跳过文件中的第一行文本(标签)
    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("学生平均分: " + t.getStudentAvg(grades) + "%\n");
      classAvg = t.getClassAvg(grades);
      System.out.println("考试平均分: " + t.getTestAvg(grades) + "%\n");
    }
    System.out.printf("\n班级平均分: %.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;
  }
}

请注意,上述代码中我对变量和函数名进行了翻译,但是代码中使用的注释部分以及格式化的输出部分仍保持原文。如果需要更多帮助,请随时提问。

英文:

The file contains the data in the following format:

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

I am trying to find out how to get the sum of each column (Ex. Test 1 = 82 + 92 + 68 + ...) to ultimately calculate the average score for each test.

This is how I parsed the file and did the other calculations:

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(&quot;TestData.txt&quot;);
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(&quot;\\|&quot;);
String name = data[0];
grades = new int[data.length - 1];
N = grades.length;
for(int i = 0; i &lt; N; i++)
{
grades[i] = Integer.parseInt(data[i + 1]);
}
System.out.println(name);
System.out.println(&quot;Student Average: &quot; + t.getStudentAvg(grades) + &quot;%\n&quot;);
classAvg = t.getClassAvg(grades);
System.out.println(&quot;Test Average: &quot; + t.getTestAvg(grades) + &quot;%\n&quot;);
}
System.out.printf(&quot;\nClass Average: %.2f%%\n&quot;, classAvg );
}
public double getStudentAvg(int[] grades)
{
double total = 0.0;
double avg = 0.0;
int N = grades.length;
for(int i = 0; i &lt; 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;
}
}

Please excuse my formatting if it's not up to par to the standard.

My main issue at the moment is how to extract the score for each test for each student and add everything up.

答案1

得分: 0

如果你已经知道文件中总共有10个测试,那就很容易了。

你可以使用 `int[] testTotal = new int[10];` 来表示 `testTotal[0] = 第一列的和`,`testTotal[1] = 第二列的和`,依此类推……

你可以逐行读取数据,并使用 `&quot;\\|&quot;` 作为正则表达式进行分割。

在遍历每一行时,你可以让 `testTotal[0] += data[1]`,`testTotal[1] += data[2]`……`testTotal[9] += data[10]`,因为 `data[0]` 是学生的姓名。
英文:

if you already know that there are 10 test total in the file , it should be easy .

you can let int[] testTotal = new int[10]; for testTotal[0] = sum-of-first-column , testTotal[1] = sum-of-second-column and so on . . .

you can read each line in and split them with &quot;\\|&quot; as regex

as you are cycling through lines , let testTotal[0] += data[1] , testTotal[1] += data[2] . . . testTotal[9] += data[10] since data[0] is the name of the student .

答案2

得分: 0

每位学生的考试成绩正在被计算。你需要的是班级平均分和考试平均分。

  • 考试平均分是每次考试的平均分数。
  • 学生平均分是每位学生的平均考试成绩。
  • 班级平均分是整个班级在所有考试中的平均表现。

考虑三次考试

            考试1    考试2   考试3   学生平均分
学生1         90      80     70          240/3
学生2        100      90     90          280/3
学生3         80      80     90          250/3

考试平均分   270/3    250/3  250/3

班级平均分 = (270 + 250 + 250)/9   或者    (240+280+250)/9      

因此,你需要以一种使其他计算变得方便的方式读取这些值。我建议使用一个Map<String, List<Integer>>来存储数据,其中字符串是学生的姓名,列表是每位学生的考试成绩。或者你可以使用一个2D int数组。但你需要保存成绩,以便可以计算考试成绩的列平均值。大部分的工作已经完成。

另外,我注意到两个问题。你调用了方法getTestAvg,但没有声明它,而且你声明了方法getClassAvg却从未调用过它。

英文:

The test scores for each student are being calculated. What you need are the class average and the test average.

  • The test averages are the average scores on each test.
  • The student averages are the average test scores for each student
  • The class averages is how the overall class did on all the tests.

Consider three tests

            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      

So you need to read in the values in such a way so as to facilitate making the other calculations. I would recommend either a Map&lt;String,List&lt;Integer&gt;&gt; for the data where the string is the students name and the list is the test scores for each student. Or you could use a 2D int array. But you need to save the scores so you can do the column average for the test scores. Most of the work is done.

Also, two problems I noticed. You call the method getTestAvg but don't declare it and you declare the method getClassAvg but never call it.

huangapple
  • 本文由 发表于 2020年8月27日 09:11:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63607758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定