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

从一个文本文件中读入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();
   }
   }
  }
  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值