Java ArrayList有两个元素,是否可以找到int元素的平均值?

Java ArrayList有两个元素,是否可以找到int元素的平均值?,java,arraylist,average,Java,Arraylist,Average,这个小项目由两个类和一个ArrayList组成。ArrayList有两个元素:一个名称和一个分数。是否可以找到元素得分的平均值 课堂教室: /** * This class creates the names and scores of the students */ public class Classroom { public int score; public String name; /** * Constructor for the Class that adds a name

这个小项目由两个类和一个ArrayList组成。ArrayList有两个元素:一个
名称
和一个
分数
。是否可以找到元素
得分的平均值

课堂
教室

/**
 * This class creates the names and scores of the students
 */
public class Classroom
{
public int score;
public String name;

/**
 * Constructor for the Class that adds a name and a score.
 */
public Classroom(String aName, int aScore)
{
    score = aScore;
    name = aName;
}

/**
 * Return the name of the students
 */
public String returnName()
{
    return name;
}

/**
 * Return the scores
 */
public int returnScore()
{
    return score;
}
}
TestScores

import java.util.ArrayList;
/**
 * Print test scrose and student names as well as he average for the class.
 */
public class TestScores
{
private ArrayList<Classroom> scores;
public int studentScores;

/**
 * Create a new ArrayList of scores and add some scores
 */
public TestScores()
{
    scores = new ArrayList<Classroom>();
}

/**
 * Add a new student and a new score.
 */
public void add (String name, int score)
{
    scores.add(new Classroom(name, score));
        if(score > 100){
            System.out.println("The score cannot be more than 100");
        }

}
import java.util.ArrayList;
/**
*打印考试成绩和学生姓名以及全班平均成绩。
*/
公开课考试成绩
{
私人ArrayList分数;
公共int学生成绩;
/**
*创建一个新的分数数组列表并添加一些分数
*/
公开考试分数()
{
分数=新的ArrayList();
}
/**
*添加一个新学生和一个新分数。
*/
公共void add(字符串名称、整数分数)
{
分数。添加(新教室(名称、分数));
如果(分数>100){
System.out.println(“分数不能超过100”);
}
}

您是否能够使用for each循环,创建一个局部变量来存储课堂上的
returnScore
方法中的学生分数,并将其除以数组大小?

简单的解决方案是在数组列表上运行for循环,并计算分数和除以数组大小

但是在你的TestScores课堂或其他课堂上这样做。课堂是个人分数,所以你不能在课堂上存储总分数或平均分数

其次,你的模型是不正确的。你称它为教室,而它只能有一个学生和分数


此外,在添加方法中,分数>100的检查是在添加分数之后进行的。如果分数不大于100,则应在添加之前进行检查,并且仅在添加到列表中。

如果在循环中为分数
中的每个元素添加
returnScore()
,并添加每个
returnScore()
转换为一个局部变量,然后将该变量除以scores.Size()
,您应该得到您想要的。如果我误解了这个问题,请告诉我。在TestScores类中执行此操作。

使用Java 8 streams,这应该可以完成工作

public double getAvg(){
    return scores.stream()
                 .mapToInt(x -> x.returnScore())
                 .average()
                 .getAsDouble();
}

另一种方法是在课堂上添加两个静态变量: totalScore和totalStudents。然后在教室的构造函数中增加这两个变量(totalScore++,totalStudents++)。 最后,您可以在课堂上创建一个静态方法,以获得平均值:

public static double getAverage(){
    return (double)totalScore/(double)totalStudents;
}

你可以遍历ArrayList,找到所有的分数,把它们加起来计算平均值……或者使用Java 8 streams,这使整个过程更加简洁。我建议你使用三种方法,看看我的答案,考虑将你的getters命名为getXxx,而不是returnXxx。当然,有了Java 8,我们现在有了streams,这将更加简单t、 当然,对于Java 8,我们有很好的解决方案。是的,我在testscores中写了一个方法。谢谢!我建议使用
教室::ReturnScores
而不是
x->x.ReturnScores()
,但这可能是个人偏好。当然,流是一个高级主题,所以我建议坚持使用“手册”首先,直到一个人精通Java。@FlorianSchaetz是的,我想先用方法引用来做,但后来改变了主意,以确保OP了解发生了什么。完全合法,但当然,因为最初的问题是“我可以使用循环来…”,我认为OP在理解这一点之前还有一段路要走;-)是的,我很抱歉我还没有学会streams,但当我把它放进去的时候它是有效的!@feelingstoned另一种方法可能是循环等。但是它太多代码,结果很小:)这种方法更好。这就是我们学习的方式。当然,请随意接受答案。所以这应该可以,但它不打印平均值,而是每个人的分数。我在这里做错了什么?public void average(){for(教室平均值:分数){System.out.println(“班级平均分是”+(average.returnScore()/scores.size());}}等等,我完全搞砸了。让我来解决它