成績統計クラス
1581 ワード
package com.xzy;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author xizhenyin
*
*/
public class ScoreCaculator {
/**
*
*/
private List<Integer> ls;
public ScoreCaculator() {
ls = new ArrayList<Integer>();
}
// **************************
//
// *************************
public void addScore(int score) {
ls.add(score);
}
// **************************
//
// *************************
public int getSumScore() {
int sum = 0;
for (Integer s : ls) {
sum = sum + s;
}
return sum;
}
public int getAverage() {
int sum = 0;
for (Integer a : ls) {
sum = sum + a;
}
return sum / ls.size();
}
public int getMaxScore() {
int max = ls.get(0);
for (int i = 0; i < ls.size(); i++) {
if (ls.get(i) > max) {
max = ls.get(i);
}
}
return max;
}
public int getMinScore() {
int min = ls.get(0);
for (int i = 0; i < ls.size(); i++) {
if (ls.get(i) < min) {
min = ls.get(i);
}
}
return min;
}
}
public class StudentScore {
public static void main(String[] args){
ScoreCaculator scl = new ScoreCaculator();
scl.addScore(89);
scl.addScore(98);
scl.addScore(100);
scl.addScore(76);
scl.addScore(70);
scl.addScore(90);
System.out.println(" "+scl.getSumScore());
System.out.println(" "+scl.getAverage());
System.out.println(" "+scl.getMaxScore());
System.out.println(" "+scl.getMinScore());
}
}