如何从列表中获取所有数字的平均值

时间:2018-05-08 00:43:12

标签: java arraylist while-loop runtime-error user-input

我需要从用户提交的数字中获得平均测试分数并保持无限。我需要知道平均考试成绩。 这是学生班:

public class Student {
    private String name;
    private int numOfQuizzes;
    private double totalScore;
    private ArrayList<Integer> scores;

    public Student(String name){
        this.name = name;
        scores = new ArrayList<Integer>();
    }

    public String getName() {
        return name;

    } public void addQuiz(int score){
        scores.add(score);
    }

    public double getTotalScore() {
        for(int score : scores){
            totalScore += score;
        }
        return totalScore;
    }

    public double getAverageScore(){
        return totalScore/(double)numOfQuizzes;
    }
}

这就是我的主要内容:

    ArrayList<String> scores = new ArrayList<String>();
    Scanner inp = new Scanner(System.in);

    // Request the name
    System.out.print("What is your name? ");
    String name = inp.nextLine();

    // Create the student object
    Student student = new Student(name);

    // Ask for the grades
    System.out.print("Please enter your scores (q to quit): ");
    String grade = inp.nextLine();

    while (!grade.equals("q")) {
        // Convert the grade to an integer and pass it 
       **student.addQuiz(Integer.parseInt(grade));**
        //this is where the error is
        // Request a new grade
        grade = inp.nextLine();
    }

    // Report the results
    System.out.println("Students Name: " + student.getName());
    System.out.println("Total Quiz Scores: " + student.getTotalScore());
    double average = student.getTotalScore()/scores.size();
    System.out.println("Average Quiz Score: " + student.getAverageScore());

这是输出:

What is your name? b
Please enter your scores (q to quit): 95
95
95
q
Students Name: b
Total Quiz Scores: 285.0
Infinity
Average Quiz Score: Infinity

正如你所看到的,即使将分数除以大小,我也会得到无穷大。

2 个答案:

答案 0 :(得分:0)

我认为你已经把自己弄糊涂了两个名单。

删除main方法中的分数列表。同时从Student类中删除totalScore;这就是所谓的“计算字段”

更新方法

public double getTotalScore() {
   double totalScore = 0;
    for(int score : scores){
        totalScore += score;
    }
    return totalScore;
}

然后你真的想要一个像这样的循环来更准确地处理输入验证的排序

String grade;
do {
    grade = inp.nextLine();
    if (grade.equals("q")) break;
    // Convert the grade to an integer and pass it 
    student.addQuiz(Integer.parseInt(grade));
} while (true);

然后,从Student类中删除numOfQuizzes

在普通方法中,这就是你使用的地方

return getTotalScore()/scores.size();

你目前正在两次获得Infinity,因为

  1. 主要方法分数永远不会添加到
  2. 您永远不会在学生课程中增加this.numOfQuizzes

答案 1 :(得分:0)

您需要对代码进行一些更改,如下所示:

学生班级:

public class Student {
    private String name;
    private double totalScore;
    private ArrayList<Integer> scores;

    public Student(String name){
        this.name = name;
        scores = new ArrayList<Integer>();
    }

    public String getName() {
        return name;

    } public void addQuiz(int score){
        scores.add(score);
    }

    public double getTotalScore() {
        totalScore = 0;
        for(int score : scores){
            totalScore += score;
        }
        return totalScore;
    }

    public double getAverageScore(){
        return getTotalScore()/scores.size();
    }
}

主要类别:

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);

        // Request the name
        System.out.print("What is your name?: ");
        String name = input.nextLine();

        // Create the student object
        Student student = new Student(name);

        // Ask for the grades
        System.out.print("Please enter your scores (q to quit): ");
        String grade = input.nextLine();

        while (!grade.equals("q")) {
            // Convert the grade to an integer and pass it 
           student.addQuiz(Integer.parseInt(grade));
            // Request a new grade
            grade = input.nextLine();
        }

        // Report the results
        System.out.println("Students Name: " + student.getName());
        System.out.println("Total Quiz Scores: " + student.getTotalScore());
        System.out.println("Average Quiz Score: " + student.getAverageScore());
    }

}

尝试使用此代码,它将正常工作并显示平均分而不是Infinity

更改完成:

  1. 学生班中删除了private int numOfQuizzes;
  2. 学生班中的totalScore = 0;方法中添加了getTotalScore()
  3. 学生班中的return getTotalScore()/scores.size();方法中添加了getAverageScore()。此处getTotalScore()返回总分和scores.size()将返回总分数。 (例如3)
  4. 主要类中删除了ArrayList<String> scores = new ArrayList<String>();。因为主要课程不需要。
  5. 已移除double average = student.getTotalScore()/scores.size();,因为下一行中的student.getAverageScore()将计算并返回平均值。