代码实现:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据 (包括学生号,姓名,三门课成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
 
/*有五个学生,每个学生有3门课的成绩,从键盘输入以上数据
 (包括学生号,姓名,三门课成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。*/
public class Test {
 
    public static void main(String[] args) throws IOException {
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        int count = 1;
        while (list.size() < 5) {
            Student s = new Student();
            System.out.println("请输入第"+count+"位学生的姓名:");
            String name = sc.nextLine();
            System.out.println("请输入"+count+"位学生的学号:");
            String no = sc.nextLine();
            System.out.println("请输入"+count+"位学生的语文成绩:");
            String ch = sc.nextLine();
            System.out.println("请输入"+count+"位学生的英语成绩:");
            String eng = sc.nextLine();
            System.out.println("请输入"+count+"位学生的数学成绩:");
            String ma = sc.nextLine();
            double chinese;
            double english;
            double math;
            try {
                chinese = Double.parseDouble(ch);
                english = Double.parseDouble(eng);
                math = Double.parseDouble(ma);
                s.setName(name);
                s.setChinese(chinese);
                s.setEnglish(english);
                s.setNo(no);
                s.setMath(math);
                list.add(s);
                count++;
            } catch (NumberFormatException e) {
                System.out.println("您都是输入有误,请重新输入");
                count--;
            }
        }
         
        writerStudent(list);
    }
 
    public static void writerStudent(ArrayList<Student> list)
            throws IOException {
        BufferedWriter bos = new BufferedWriter(new FileWriter("stud"));
 
        for (int i = 0; i < list.size(); i++) {
            bos.write("姓名:" + list.get(i).getName() + " 学号:" + list.get(i).getNo()
                    + " 语文成绩:" + list.get(i).getChinese() + " 数学成绩"
                    + list.get(i).getMath() + "英语成绩:" + list.get(i).getEnglish()
                    + " 平均成绩:" + list.get(i).getAvg());
            bos.newLine();
        }
        bos.close();
    }
}

 

posted on   LoaderMan  阅读(3886)  评论(0编辑  收藏  举报

努力加载评论中...

导航

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示