* 题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件
public class 第五十题保存学生成绩 { public static void main(String[] args) throws IOException { //保存学生成绩到文件stud中 int stuId = 0; //学号 String stuName = null;//姓名 int grade1 = 0;//第一门课成绩 int grade2 = 0;//第二门课成绩 int grade3 = 0;//第三门课成绩 String s = ""; Scanner in = new Scanner(System.in); File file = new File("D:\\stud.txt"); for(int i = 1; i < 4; i++) { System.out.println("请输入第"+i+"个学号,姓名,和三门课的成绩,以空格隔开"); stuId = in.nextInt(); stuName = in.next(); grade1 = in.nextInt(); grade2 = in.nextInt(); grade3 = in.nextInt(); s = s + "\r\n"+ "学号:"+stuId+"姓名:"+stuName+"语文:"+grade1+"数学:"+grade2+"英语:"+grade3; } byte[] contentInBytes = s.getBytes(); try { OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); out.write(contentInBytes); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } in.close(); } }
原文地址:https://www.cnblogs.com/zjulanjian/p/10952755.html
时间: 2024-10-27 13:00:40