有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括姓名,三门课成绩),计算出平均成绩,并把学生的信息和计算出的平均分数存放在磁盘文件"stud.txt"中.

解决方案 »

  1.   


    import java.io.FileOutputStream;
    import java.util.Scanner;
    /**
     * Stud.java
     * 2007-10-7 上午09:48:34
     * TODO
     *//**
     * @author ustbsjl
     * 
     */
    public class Stud { /**
     * @param args
     */
    public static void main(String[] args) throws Exception{
    // TODO Auto-generated method stub
    String name = null;
    double firstScore = 0.0;
    double secendScore = 0.0;
    double thirdScore = 0.0;
    double avg = 0.0;
    StringBuffer sb = new StringBuffer("[Stu info]\n");
    Scanner sc = new Scanner(System.in);
    for (int i = 1; i <= 2; i++) {
    System.out.println("please enter stu " + i + " info...");
    System.out.print("Name: ");
    name = sc.next();
    System.out.print("FirstScore: ");
    firstScore = sc.nextDouble();
    System.out.print("SecendScore: ");
    secendScore = sc.nextDouble();
    System.out.print("ThirdScore: ");
    thirdScore = sc.nextDouble();
    avg = (firstScore + secendScore + thirdScore) / 3;
    System.out.println("This stu's Avg score is " + avg);
    sb.append("Name: " + name + ", FirstScore: " + firstScore
    + ", SecendScore: " + secendScore + ", ThirdScore: "
    + thirdScore + ", Avg: " + avg + "\n");
    }
    FileOutputStream fout = new FileOutputStream("stud.txt");
    fout.write(sb.toString().getBytes());
    }}
      

  2.   

    用标准的javaio流读取控制台的输入的数据,然后用文件流写文件。
    你还是自己动手写一下,记忆深刻些。