建立一个高考分数(总分)统计系统,它能计算所有考生的平均分,总分,以及给出一个分数段,列出在这个区间类所有的考生人数和名单。
这是由许多个同学,上一个是统计一个学生的系统。所以要建Student类。
学生类:
package com.xzy;
public class Student {
private String name;
private int score;
public Student(String name,int score){
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String toString(){
return "姓名"+name +"分数"+score;
}
}
统计类:
package com.xzy;
import java.util.ArrayList;
import java.util.List;
public class Caculator {
private List<Student> students;
public Caculator(){
students = new ArrayList<Student>();
}
// 加入学生
public void addStudent(Student student){
students.add(student);
}
//求总分
public int getSumScore(){
int sum = 0;
for(Student s:students){
sum = sum + s.getScore();
}
return sum;
}
//平均分
public int getAverageScore(){
int sum = 0;
for(Student s:students){
sum = sum + s.getScore();
}
return sum/students.size();
}
//统计区间学生信息
public int betweenScore(int min,int max){
System.out.println("分数在"+min+" 和"+max+"之间");
int count = 0;
for(Student s:students){
if(s.getScore()>min&&s.getScore()<max){
System.out.println(s);
count++;
}
}
return count;
}
}
Main 输入学生信息
package com.xzy;
public class StudentInfo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Caculator caculator = new Caculator();
caculator.addStudent(new Student("Andy",450));
caculator.addStudent(new Student("Babi",467));
caculator.addStudent(new Student("Cidy",502));
caculator.addStudent(new Student("Duck",425));
caculator.addStudent(new Student("Girl",530));
caculator.addStudent(new Student("Hill",401));
caculator.addStudent(new Student("Milu",433));
System.out.println("平均分数是"+caculator.getAverageScore());
System.out.println("总分是"+caculator.getSumScore());
System.out.println("共"+caculator.betweenScore(450, 550)+"人");
}
}
结果:
平均分数是458
总分是3208
分数在450 和550之间
姓名Babi分数467
姓名Cidy分数502
姓名Girl分数530
共3人