您的位置:首页 > 编程语言 > Java开发

Java-文本文件中读入30个学生的姓名和成绩

2013-05-14 11:46 766 查看
从一个文本文件中读入30个学生的姓名和成绩,计算所有学生的最高分、最低分和平均分,并将结果写入另外一个文件。
  程序代码:
  import java.io.*;
  public class Stdfile {
   static int max(int a[], int n) //最高分
   {
   int temp = a[0]; //临时变量
   for(int i = 1; i < n; i++)
   if(a[i] > temp)
   temp = a[i];
   return temp;
   }
   static int min(int a[], int n) //最低分
   {
   int temp = a[0];
   for(int i = 1; i < n; i++)
   if(a[i] < temp)
   temp = a[i];
   return temp;
   }
   static int average(int a[], int n) // 求平均值
   {
   int temp = 0;
   for(int i = 0; i < n; i++)
   temp += a[i];
   return temp/n;
   }
   public static void main(String args[]){
   try{
   File fin = new File("Std1.txt");
   File fout = new File("Std2.txt");
   BufferedReader in = new BufferedReader(new FileReader(fin)); //读文件Std1.txt
   PrintWriter out = new PrintWriter(new FileWriter(fout)); //写到Std2.txt
   int[] scores = new int[30];
   int flag = 0;
   int num = 0;
   String s = in.readLine();
   flag++;
   while(s != null){
   if(flag%2==0){
   scores[num] = Integer.valueOf(s);
   num++;
   }
   s = in.readLine();
   flag++;
   }
   int maxValue = max(scores, 30);
   int minValue = min(scores, 30);
   int averageValue = average(scores, 30);
   System.out.println("所有学生的最高分是:"+maxValue);
   System.out.println("所有学生的最低分是:"+minValue);
   System.out.println("所有学生的平均分是:"+averageValue);
   }catch(FileNotFoundException e){
   System.err.println("File Not Found!");
   }
   catch(IOException e){
   e.printStackTrace();
   }
   }
  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐