* 題目:有五個學生,每個學生有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(); } }