java基础编程

java基础编程第50道
题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件”stud”中。



import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class Test{
    final static int stu_num=5;
    final static int cou_num=3;

    public static void main(String[] args){
        Stu stu[]=new Stu[5];
        Scanner sc=new Scanner(System.in);
        for(int i=0;inew Stu();
            System.out.println("请输入第"+(i+1)+"个学生的姓名");
            String name=sc.next();
            stu[i].setName(name); 
            System.out.println("请输入第"+(i+1)+"个学生的学号");
            stu[i].setStu_num(sc.next());
            for(int j=0;j"请输入第"+(i+1)+"个学生的第"+(j+1)+"科的名称");
                String course=sc.next();
                System.out.println("请输入第"+(i+1)+"个学生的"+course+"的分数");
                int scor=sc.nextInt();
                stu[i].setScore(course, scor);
            }
            stu[i].cal(stu[i]);
        }
        try {
            compositor(stu);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        sc.close();
    }
    public static void compositor(Stu[] stu) throws IOException{
        FileWriter fw=new FileWriter("E://stud.txt");
        BufferedWriter bw=new BufferedWriter(fw);
        for(int i=0;i":");
            bw.write(stu[i].getStu_num());
            bw.write(stu[i].getScore().toString());
            bw.newLine();
        }
        bw.flush();
        bw.close();
    }
}

class Stu{
    private String name,stu_num;
    private Collection arr=new ArrayList();
    private Map score=new HashMap();
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getStu_num() {
        return stu_num;
    }
    public void setStu_num(String stu_num) {
        this.stu_num = stu_num;
    }
    public int getScore(String course){
        return score.get(course);
    }
    public void setScore(String course,int sco){
        score.put(course, sco);
    }
    public Map getScore(){
        return score;
    }
    public void cal(Stu stu_1){
        arr=stu_1.getScore().values();
        int sum=0,j=0;
        for(int i:arr){
            j++;
            sum+=i;
        }
        setScore("平均分",(int)sum/j);
    }
}

你可能感兴趣的:(java)