有五个学生,每个学生有3门课的成绩,
从键盘输入以上数据(包括学生号,姓名,三门课成绩),
计算出平均成绩,把原有的数据和计算出的平均分数存放在磁盘文件 "stud "中。
- public static void main(String[] args)throws IOException
- {
- Scanner sc = new Scanner(System.in);
- BufferedWriter bufw = new BufferedWriter(new FileWriter("stud.txt"));
- float chinese = 0,maths = 0,english = 0;
- float chinAve = 0;
- float mathAve = 0;
- float englAve = 0;
- for (int x =1;x<=5 ;x++ )
- {
- System.out.println("请输入学号");
- int number = sc.nextInt();
- System.out.println("请输入姓名");
- String name = sc.next();
- System.out.println("请输入语文成绩");
- chinese = sc.nextFloat();
- chinAve = chinAve + chinese;
- System.out.println("请输入数学成绩");
- maths = sc.nextFloat();
- mathAve = mathAve + maths;
- System.out.println("请输入英语成绩");
- english = sc.nextFloat();
- englAve = englAve + english;
- bufw.write("学生"+x+":");
- bufw.newLine();
- bufw.write("学号:"+number+"姓名:"+name+"语文:"+chinese+"数学:"+maths+"英语:"+english);
- bufw.newLine();
- }
- chinAve = chinAve/5;
- mathAve = mathAve/5;
- englAve = englAve/5;
- bufw.write("平均分:");
- bufw.newLine();
- bufw.write("语文:"+chinAve+"数学:"+mathAve+"英语:"+englAve);
- bufw.close();
-
- }
复制代码 |
|