日期:2014-05-20  浏览次数:20726 次

刚学JAVA,有点地方不太明白,希望大虾帮忙写来参考下!!!谢谢
有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括姓名,三门课成绩),计算出平均成绩,并把学生的信息和计算出的平均分数存放在磁盘文件"stud.txt"中.

------解决方案--------------------
Java code

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

}