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

解决方案 »

  1.   


    import java.io.*;class Score{
    String[] name = new String[5];
    double[] score = new double[5];
    double[] sum = new double[5];

    public void input() throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    boolean change =true;
    while(change){
    try{
    for(int i = 0;i <= 4;i++){
    System.out.print("Input the name : ");
    name[i] = br.readLine();
    for(int j = 0;j <= 2;j++){
    System.out.print("Input "+(j+1)+" score : ");
    score[j] = Integer.parseInt(br.readLine());
    }
    System.out.println();
    sum[i] = score[0]+score[1]+score[2];
    }
    change =false;
    }
    catch(NumberFormatException nfe){
    System.out.println("Sorry,The Score only can input a number,Please input again.");
    }
    }
    }

    public void compositor() throws IOException{
    FileWriter fw = new FileWriter("F://Socre.txt");     //Îļþ·ÅÓÚfÅÌÏ£¬ÎļþÃûΪ"Socre.txt".
    BufferedWriter bw = new BufferedWriter(fw);
    double temp1;
    String temp2;
    for(int i = 0;i < 4;i++){
    for(int j = 1;j < 5-i;j++){
    if(sum[j-1] > sum[j]){
    temp1 = sum[j];
    sum [j] = sum[j-1];
    sum[j-1] = temp1;
    temp2 = name[j];
    name[j] = name[j-1];
    name[j-1] = temp2;
    }
    }
    }
    for(int i = 0;i <= 4;i++){
    bw.write(name[4-i]);
    bw.write(" "+sum[4-i]);
    bw.newLine();
    }
    bw.close();
    }

    public static void main(String[] args) throws IOException{
    Score s = new Score();
    s.input();
    s.compositor();
    }
    }
      

  2.   

    对不起,文件名没看清楚.import java.io.*;class Score{
    String[] name = new String[5];
    double[] score = new double[5];
    double[] sum = new double[5];

    public void input() throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    boolean change =true;
    while(change){
    try{
    for(int i = 0;i <= 4;i++){
    System.out.print("Input the name : ");
    name[i] = br.readLine();
    for(int j = 0;j <= 2;j++){
    System.out.print("Input "+(j+1)+" score : ");
    score[j] = Integer.parseInt(br.readLine());
    }
    System.out.println();
    sum[i] = score[0]+score[1]+score[2];
    }
    change =false;
    }
    catch(NumberFormatException nfe){
    System.out.println("Sorry,The Score only can input a number,Please input again.");
    }
    }
    }

    public void compositor() throws IOException{
    FileWriter fw = new FileWriter("F://stud.txt");     //文件存放于f盘下的"stud.txt"文件下;
    BufferedWriter bw = new BufferedWriter(fw);
    double temp1;
    String temp2;
    for(int i = 0;i < 4;i++){
    for(int j = 1;j < 5-i;j++){
    if(sum[j-1] > sum[j]){
    temp1 = sum[j];
    sum [j] = sum[j-1];
    sum[j-1] = temp1;
    temp2 = name[j];
    name[j] = name[j-1];
    name[j-1] = temp2;
    }
    }
    }
    for(int i = 0;i <= 4;i++){
    bw.write(name[4-i]);
    bw.write(" "+sum[4-i]);
    bw.newLine();
    }
    bw.close();
    }

    public static void main(String[] args) throws IOException{
    Score s = new Score();
    s.input();
    s.compositor();
    }
    }
      

  3.   

    而且在class前面加上public,晚上做有点累了,所以有点急,嘿嘿,想睡觉
      

  4.   

    我也是初学者,今天刚想出来的一个简单的程序。你参考参考。写的不好import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;public class Demo50 { /**
     * 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩)
     * ,计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件 "stud "中。
     * 
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    System.out.println("请依次输入学生号,姓名,三门课成绩,用逗号隔开,每个学生之间用句号隔开");
    String str = input.next();
    String[] student = str.split("\\.");
    BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\stud.txt"));
    for (int i = 0; i < student.length; i++) {
    String[] scores = student[i].split("\\,");
    bw.write(student[i]);
    int[] num1 = new int[3];
    int sum = 0;
    for (int m = 0; m < 3; m++) {
    num1[m] = Integer.parseInt(scores[m + 2]);
    sum += num1[m];
    }
    bw.write(".平均分:" + sum/3);
    System.out.println(sum/3);

    }
    bw.flush();
    bw.close();
    }}
      

  5.   

    bw.write(".平均分:" + sum/3);
    这里会丢失精度,算出来肯定是个整数。比如211/3=70;小数部分丢了
      

  6.   

     int sum = 0;
    这里定义为:double sum ;