import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.util.Scanner;
/*题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计
算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件"stud"中
*/
public class ArraysDemo { public static void main(String[] args) { Student[] students = new Student[2]; for(int i=0;i<students.length;i++) { Scanner in = new Scanner(System.in); System.out.println("输入第"+(i+1)+"个学生学号"); String id = in.nextLine(); System.out.println("输入姓名"); String name = in.nextLine(); System.out.println("输入语文成绩"); int chinese = in.nextInt(); System.out.println("输入数学成绩"); int math = in.nextInt(); System.out.println("输入英语成绩"); int english = in.nextInt(); Student student = new Student(); student.id=id; student.name=name; student.chinese=chinese; student.math=math; student.english=english; student.avg=(chinese+math+english)/3; students[i]=student; } for(int i=0;i<students.length;i++) { System.out.println(students[i]); } File file= new File("f:\\Score.txt"); if(file.exists()){ System.out.println("文件存在"); } else{ System.out.println("文件不存在,正在创建...."); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } FileOutputStream fileout=null; OutputStreamWriter output=null; try { fileout = new FileOutputStream(file); output = new OutputStreamWriter(fileout, "gbk"); output.write("学号\t"+"姓名\t"+"语文\t"+"数学\t"+"英语\t"+"平均值\t"); for(int i=0;i<students.length;i++) { output.write("\r\n"+students[i].id+"\t"+students[i].name+"\t"+students[i].chinese +"\t"+students[i].math+"\t"+students[i].english+"\t"+students[i].avg); output.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { output.close(); fileout.close(); }catch (Exception e) { } } } }