编写一个程序:要求(从标准输入中)输入五个学生的成绩(从0到100的整数),并将这五个数保存到文件“data.txt”中。
    然后再编写一个程序:从文件“data.txt”中读取这五个学生的成绩,并计算它们平均数,然后按从小到大的顺序(在标准输出窗口)输出这五个学生的成绩。

解决方案 »

  1.   

    public static void saveScore()
    {
    int countOfStudent=5;
        try {
        BufferedWriter out = new BufferedWriter(new FileWriter("data.txt"));
    BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));    for(int i=0;i<countOfStudent;i++){
            out.write(Float.parseFloat(keyboard.readLine().trim()));
            out.write("\n");
        }
        out.close();
        } catch (IOException e) {
        //IOException happened!
    System.out.println("Exception was thrown when saving into file!");
        }
    }
      

  2.   

    public static void readScore()
    {
        int countOfStudent=5,i=0;
        float score[]=new float[countOfStudent];
        try {
        BufferedReader in = new BufferedReader(new FileReader("company.record"));    while ((str=in.readLine()) != null&&i++<countOfStudent) {
            score[i] = Float.parseFloat(in.readLine().trim());
        }
        in.close();
        } catch (IOException e) {
        //IOException happened!
            System.out.println("Exception was thrown when reading from file!");
        }
        //求平均值,排序我就不写了~
        .....
    }
      

  3.   

    编译显示write(float)错误  怎么回事啊
      

  4.   

    疏忽了:
    out.write(""+Float.parseFloat(keyboard.readLine().trim()));
    或者用:
    out.write(keyboard.readLine().trim());
    也可以,如果保证你输入的是合法数字的话;不合法上面那种会抛出异常~
      

  5.   

    后面那个:
    public static void readScore()
    {
        int countOfStudent=5,i=0;
        float score[]=new float[countOfStudent];
        try {
        BufferedReader in = new BufferedReader(new FileReader("data.txt"));    while ((str=in.readLine()) != null&&i++<countOfStudent) {
            score[i] = Float.parseFloat(str.trim());
        }
        in.close();
        } catch (IOException e) {
        //IOException happened!
            System.out.println("Exception was thrown when reading from file!");
        }
        //求平均值,排序我就不写了~
        .....
    }
      

  6.   

    str没有定义 我定义了一个不能运行 好象是说empty string
      

  7.   

    我也是自己搞了半天 做不出来才在这上面发帖子的啊 其实第一个我已经做出来了 就是第二个 怎么把数据一个一个从文件中读出来 再要对每个数据一一处理不会 我把第二个贴出来 大家帮我改下吧
    //J_Test013.java
    import java.io.*; 
    public class J_Test013
    {
        public static void main(String args[])
        {
            String currentLine[]=new String[5]; 
            int s[]=new int[5];
            for (int i=0; i < args.length; i++)
            {
                System.err.println("Contents of file: " + args[i] + " are:");
                try
                {
                    LineNumberReader br = 
                    new LineNumberReader(new FileReader(args[i]));
                    int j=0;
                    while (br.readLine() != null)
                      currentLine[j] = br.readLine();  
                        s[j]=Integer.parseInt(currentLine[j]);
                        j++;
                    int m=0;
                    for(j=0;j<5;j++)
                       for(int k=0;k<5;k++)
                       {
                           if(s[k]>s[k+1])
                           m=s[k];
                           s[k]=s[k+1];
                           s[k+1]=m;
                       }
                        // currentLine[i]=String.valueOf(s[i]);   
                       for(j=0;j<5;j++)
                       {
                           currentLine[i]=String.valueOf(s[i]);
                           System.out.println(br.getLineNumber() + ": " + currentLine[i]);
                       }
                      }
                catch (IOException e)
                {
                    System.err.println("Error: " + e);
                } 
            } 
        } 
    } // End of class: J_Test013
    编译没有错 运行也没错 就是没有结果