创建一个代表测试分数平均值的课程

时间:2018-04-06 15:20:43

标签: java

创建一个代表测试分数平均值的课程。让课程获得无限数量的分数,并计算测试次数和平均分数。

我似乎无法找到存储所有测试分数的方法,也无法计算给定文件中的测试数量。目前,此代码仅计算其中一个测试,并且仅存储给定的最后一个数字。

enter image description here

此文件由教师提供,无法更改

public class TestScoresDemo {

    public static void main(String[] args) {
        TestScores t1 = new TestScores("Alice");
        TestScores t2 = new TestScores("Bob");

        t1.addTestScore(50);
        t1.addTestScore(60);
        t1.addTestScore(54);
        t1.addTestScore(73);
        t1.addTestScore(88);
        t1.addTestScore(92);

        t2.addTestScore(87);
        t2.addTestScore(97);
        t2.addTestScore(37);
        t2.addTestScore(99);

        System.out.println("-- Alice --");
        System.out.println("Num tests taken: " + t1.getNumTestsTaken());
        System.out.println("Average: " + t1.getAverage());

        System.out.println("-- Bob --");
        System.out.println("Num tests taken: " + t2.getNumTestsTaken());
        System.out.println("Average: " + t2.getAverage());
    }
}

教师给出的文件结尾。 以下是我到目前为止的情况。

import java.util.Scanner;

public class TestScores {
    private String Name;
    private double TotalScore;
    private int NumScores;
    private double Earned;

    public TestScores(String name) {
        Name = name;
    }

    public void addTestScore(double earned) {
        Earned = earned;
    }

    public int getNumTestsTaken() {

        NumScores = 0;
        while (Earned < 100.0) { 
            NumScores++;
        }
        return NumScores;
    }

    public double getAverage() {
        try (Scanner scanner = new Scanner(Name)) {
            double sum = 0.0;
            while (Earned <100.0) {
                sum += Earned;
                TotalScore = sum / NumScores;
                break;
            }
            return TotalScore;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您没有正确使用变量NumScores或TotalScore。此外,您应该使用变量的Java命名约定,因此将名称中的第一个字母更改为小写字母,然后使用国会大厦字母,AKA驼峰案例。请尝试以下

public TestScores(String name) {
    this.name = name;
    numScores = 0;
    totalScore = 0.0;
}

public void addTestScore(double earned) {
    totalScore += earned; //add to the running total
    numScores++; //add to the count
}

public int getNumTestsTaken() {
    return numScores; //simply return the counter
}

public double getAverage() {
    return totalScore/numScores; //return the total scores added up divided by the count
}

答案 1 :(得分:0)

有两种方法可以解决这个问题,两者都在上面的评论中讨论过:

  1. 使用list / array / collection存储每个值
  2. 添加新元素时加班的数字
  3. 正如您的要求所说,TestScores类无法更改,那么它会从可能的解决方案 1 中留下第一种方法,这使得第二种方法唯一可行的答案。

    但在写下答案之前,我将拯救@Turing85的comment

      

    对整体代码设计的评论:变量和字段名称应始终以小写字母开头(private String Name; - &gt; private String name;)。

    因此,您的TestScores属性可能会变为:

    private String name;
    private double totalScore;
    private int numScores;
    private double earned;
    

    这将使您和我们所有人更容易阅读您的程序。阅读并关注Java naming conventions

    现在让我们看看你的程序现在正在做什么:

    t1.addTestScore(50); //earned = 50
    t1.addTestScore(60); //earned = 60
    t1.addTestScore(54); //earned = 54
    

    你想要什么:

    t1.addTestScore(50); //earned = 50
    t1.addTestScore(60); //earned = 110
    t1.addTestScore(54); //earned = 164
    

    您需要做的就是(在将变量名更改为小写字母后):

    public void addTestScore(double earned) {
        this.earned += earned; //This will be adding every number you send to it to the total
        this.numScores++; //This keeps track of the number of tests taken
    }
    

    现在getNumTestsTaken方法可能会变成:

    public int getNumTestsTaken() {
        return numScores;
    }
    

    你的getAverage方法会变成这样:

    public double getAverage() {
        return earned / numScores; //We already know the total amount earned as it's stored in "earned" variable and the number of scores stored in "numScores" so, this becomes a simple operation
    }
    

    对于其余的代码,我认为没问题。

    1 我要从这个答案中删除数组/集合,但是如果有人想要添加它以及答案,那么将这个答案作为社区维基。但是我已经在这里解释了为什么这不适合(根据OP的要求)。