繁体   English   中英

如何在Java的ArrayList中找到平均值和最高评分?

[英]How do I find the average and the top grade in an ArrayList in Java?

在这段代码中,我想找出一个学生获得的最高分数,以及所有分数的平均值。 通过用户输入将标记放入ArrayList中。 我已经完成了一半的双打,但是不知道如何完成它,我想知道如何找到最高分。

谢谢。

import java.util.*;
import java.lang.*;
class Course
{
    private ArrayList<Student> people = new ArrayList<Student>();

    public void add( Student s )
    {
        people.add( s );
    }
    //Return the number of students who passed (mark>= 40)
    public int pass()
    {
        int count = 0;
        for ( int i=0; i < people.size(); i++ )

        {
            int mark = people.get(i).getMark();
            if(mark < 40){
                count = count +1;
            }
        }

        return count;
    }

    public int fail()
    {
        int count = 0;
        for ( int i=0; i < people.size(); i++ )

        {
            int mark = people.get(i).getMark();
            if(mark < 40){
                count = count +1;
            }
        }

        return count;
    }

   public String top()
   {

   }

    public double average()
    {
        int sum=0;

        for (int i=0; i < people.size(); i++ )
        {
            double average = sum / (double) i;
        }

        return sum;
    }

}

使用Collections.max(people, yourComparator) ,其中yourComparator使用getMark作为比较字段:您可以执行以下操作:

Student maxStudent = Collections.max(people, new Comparator<Student>() {
    @Override
    public int compare(Student first, Student second) {
        if (first.getMark() > second.getMark())
            return 1;
        else if (first.getMark() < second.getMark())
            return -1;
        return 0;
    }
});

为了获得最高分,您创建了一个变量,每次找到较高分时都将其替换。 如果您已经在循环,则可以通过将循环中的所有标记相加然后除以总人数来找到平均值。 在这里,我在同一个循环中都完成了这两项:

int totalMark = 0;
int topMark = 0;
for (int i=0; i< people.size(); i++) {
    int mark = people.get(i).getMark(); 
    if (mark > topMark) {
        topMark = mark;
    }
    totalMark += mark;
}
int averageMark = totalMark / people.size();

您可以通过在add(Student)方法中收集统计信息来完成此操作:

public class Course {
    private ArrayList<Student> people = new ArrayList<Student>();
    private int passing = 0;
    private int failing = 0;
    private int top = Integer.MIN_VALUE;
    private int sum = 0;

    public void add( Student s ) {
        people.add( s );
        if(s.getMark() >= 40){
            passing++;
        }
        else {
            failing++;
        }
        sum += s.getMark();
        if(s.getMark() > top) {
            top = s.getMark();
        }
    }

    public int pass() {
        return passing;
    }

    public int fail() {
        return failing;
    }

    public int top() {
        return top;
    }

    public double average() {
        return sum / people.size();
    }
}

要直接回答您的问题,只需将每个标记与找到的最大标记进行比较即可找到顶部。 通过将总和除以分数得出平均值。

您可以在Student类中实现Comparable接口( http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html ),然后使用Collectionshttp://docs.oracle.com)。 com / javase / 1.4.2 / docs / api / java / util / Collections.html )类,以使用maxmin函数来获取max和min等级值。 要获得平均成绩值,您只需要迭代ArrayList并得出成绩值的总和,最后将总和除以ArrayList.size()

检查一下。 它可能会帮助您。

Java程序查找除前三名外的所有数字的平均值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM