关于计算总分与平均分的问题!
提示: 作者被禁止或删除 内容自动屏蔽
public class SumAndAvgImpl extends SumAndAvg{ public static void main(String[] args) { Student s1=new Student(1,"1",90); Student s2=new Student(1,"1",91); Student s3=new Student(1,"1",92); Student s4=new Student(1,"1",93); Student s5=new Student(1,"1",94); Student s6=new Student(1,"1",95); Student s7=new Student(1,"1",96); SumAndAvg saa = new SumAndAvgImpl(); saa.addStus(s1); saa.addStus(s2); saa.addStus(s3); saa.addStus(s4); saa.addStus(s5); saa.addStus(s6); saa.addStus(s7); System.out.println(saa.getAvg()); System.out.println(saa.getSum()); System.out.println(saa.getSum(2,4)); System.out.println(saa.getAvg(2,4)); } @Override public float getAvg() { return getSum()/getStus().size(); } @Override public float getAvg(int startN, int endN) { return getSum(startN, endN)/(endN-startN+1); } @Override public float getSum() { float sum=0; for(Student stu:getStus()){ sum+=stu.getScore(); } return sum; } @Override public float getSum(int startN, int endN) { float sum=0; for(int i = startN;i<=endN;i++){ sum = sum+getStus().get(i-1).getScore(); } return sum; } } public class Student { private float num; private String name; private float score; public Student(){ } public Student(int num, String name, int score){ this.num=num; this.name=name; this.score=score; } public float getNum() { return num; } public void setNum(int num) { this.num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getScore() { return score; } public void setScore(int score) { this.score = score; } } import java.util.ArrayList; public abstract class SumAndAvg { private static ArrayList<Student> stus=new ArrayList<Student>();; public abstract float getSum(); public abstract float getAvg(); public abstract float getSum(int startN, int endN); public abstract float getAvg(int startN, int endN); public SumAndAvg(){ } public void addStus(Student stu){ stus.add(stu); } public ArrayList<Student> getStus(){ return stus; } }代码没有判断是否溢出,楼主自己修改