我的Java学习-学生成绩管理简单编程

java的应用貌似在数据和网络方面比较多,学习过程中遇到的问题也经常与数据管理方面有关。这里有简单的成绩管理应用,非图形界面。

class Student{
    private String name;
    private String ID;
    private double[] score;
    public Student(String name, String ID){
        this.name = name; 
        this.ID = ID;
    }
    public String getName(){
        return name;
    }
    public String getID(){
        return ID;
    }
    public boolean setScore(double fenshu[]){  //判断当分数的输入是合理时,对数组赋值
        boolean set = true;
        score = new double[fenshu.length];
        for(int i = 0; i < fenshu.length; i++){
            if(fenshu[i] < 0 || fenshu[i] > 100){
                set = false;                
            }else{
                score[i] = fenshu[i];               
            }                                           
        }
        return set;
    }
    public double[] getScore(){
        return score;
    }
    public double sum(){   //求总分
        double sum = 0;
        for(int i = 0; i < score.length; i++){
            sum += score[i];
        }
        return sum; 
    }
    public double average(){  //求平均分
        double average = this.sum() / score.length;
        return average;
    }
    public double[] max_min(){   //求最高分和最低分
        double[] max_min = new double[2];
        max_min[0] = 0;
        max_min[1] = score[0];
        for(int i = 0; i < score.length; i++){
            if(score[i] > max_min[0]){
                max_min[0] = score[i];
            }
            if(score[i] < max_min[1]){
                max_min[1] = score[i];
            }
        }
        return max_min;
    }
    
    public void prinf(){
        System.out.println(name + ID +"成绩的总分是:" + this.sum());
        System.out.println("       成绩的平均分是: " + this.average());        
        System.out.println("       最高分是:"+ this.max_min()[0]);
        System.out.println("       最低分是:"+ this.max_min()[1]);                   
    }
}

public class Test{
    public static void main(String[] args){
        Student std = new Student("张三", "201607");
        double[] fenshu ={56, 77, 89};
        if(std.setScore(fenshu))
        	std.prinf();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43399072/article/details/83211640
今日推荐