代码之家  ›  专栏  ›  技术社区  ›  b w

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

  •  -3
  • b w  · 技术社区  · 6 年前

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

    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 回复  |  直到 6 年前
        1
  •  0
  •   Nitin Bisht    6 年前

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

    学生班级:

    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() 在下一行中,将计算并返回平均值。
        2
  •  0
  •   OneCricketeer Gabriele Mariotti    6 年前

    我想你已经把自己弄糊涂了,因为你有两张清单。

    删除main方法中的分数列表。同时删除 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);
    

    然后,删除 numOfQuizzes 来自学生班。

    在平均法中,这就是你使用的

    return getTotalScore()/scores.size();
    

    你现在得到无穷大两次,因为

    1. 主方法得分从未添加到
    2. 你永远不会递增 this.numOfQuizzes 在学生课堂上