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

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 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 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();

}

}


版权声明:本文为weixin_42284380原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。