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();
}
}
}
程序代码:
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();
}
}
}
相关文章推荐
- 键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
- Java基础知识强化之集合框架笔记49:键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台
- 假设有10位同学,各有3门课程的成绩。从键盘输入每位同学的学号、姓名和3门课程的成绩,然后把数据保存到磁盘文件,再将磁盘文件中的学生信息读入并显示
- Student类,包括学生姓名、性别、年龄、Java成绩。要求创建五个该类对象,输出每个学生信息,计算并输出这五个学生Java成绩的平均值, 以及计算并输出他们Ja
- 【程序50】TestStu.java 题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩), //计算 出平均成绩,况原有的数据和计算出的平均分数存放在磁盘
- java 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
- 读入n名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。
- 有10个学生,每个学生的数据包括学号、姓名、3门课程的成绩。读入这10个学生的数据,要求输出3门课程的总平均成绩,以及个人平均分最高的学生的数据
- 用java面向对象的内容建立学生姓名,学号,id,班级,每一科的成绩,以及对管理员实现(对老师只实现查看):通过名字查询成绩,通过学号查询成绩,通过id修改姓名,通过姓名修改成绩
- java从文本文件中一次读入一行数据进行处理
- JAVA语言 实现简单的学生成绩管理系统(总分+平均分+排名)
- 给出n个学生的考试成绩表,每条记录由学号、姓名和分数和名次组成,设计算法完成下列操作: (1)设计一个显示对学生信息操作的菜单函数如下所示: *************************
- 【C语言】有n个结构体变量,内含学生号, 姓名和三门课成绩。要求输出平均成绩最高学生的信息。
- 第十四周项目2--成绩处理(改进版,能输入,输出学生姓名)
- JAVA写的学生成绩管理系统
- PHP之键盘输入四个学生的学号 姓名 语文 数学 英语信息 ,计算出每个学生的平均成绩,输出
- 有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序
- 输入五位学生姓名成绩,输出最大值最小值
- 2 编写一个应用程序,用户分别从两个文本框输入学生的姓名和分数,程序按成绩排序将这些学生的姓名和分数显示在一个文本区中。 程序运行效果如图:
- 读入N名学生的成绩,将获得某一给定分数的学生人数输出