/*
2017年3月13日16:17:38
java基础50道经典练习题 例50
Athor: ZJY
Purpose:
【程序50】
题目:有五个学生,每个学生有3门课的成绩,从键盘输入
以上数据(包括学生号,姓名,三门课成绩),计算出平均
成绩,将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
*/
import .*;
class Students { //定义学生模型
public int id; //学生的学号
public String name; //学生的姓名
public float[] grade = new float[3]; //学生的三门成绩
public float sum; //学生三门成绩之和
public Students(int id,
String name,
float[] grade,
float sum) {
= id;
= name;
[0] = grade[0];
[1] = grade[1];
[2] = grade[2];
= sum;
}
public static void traverse(Students stud) {
(+" "++" "+[0]
+" "+[1]+" "+[2]+" "+/3);
}
}
public class ProgramNo50_1
{
public static void main(String[] args) throws IOException {
Students[] stud = new Students[5];
infoInput(stud);
infoOutput(stud);
}
//学生信息的输入 学号 姓名 成绩
private static void infoInput(Students[] stud) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader());
int id = 0;
String name = null;
float[] grade = new float[3];
float sum = 0;
boolean isRecord = true;
try {
while (isRecord) {
for (int i=0; i<; i++) {
("输入第"+(i+1)+"名学生的学号: ");
id = (());
("输入第"+(i+1)+"名学生的姓名: ");
name = ();
sum = 0;
for (int j=0; j<; j++) {
("输入第"+(i+1)+"名学生的第"+(j+1)+"门成绩: ");
grade[j] = (());
sum += grade[j];
}
();
stud[i] = new Students(id, name, grade, sum);
}
isRecord = false;
}
();
}catch(NumberFormatException e) {
();
}
}
//学生信息的输出 学号 姓名 成绩
private static void infoOutput(Students[] stud) throws IOException {
FileWriter fw
= new FileWriter("G:/Project/java_object/java基础50道经典练习题/topic50/学生信息");
BufferedWriter bw = new BufferedWriter(fw);
(" NO. "+" name "+" grade1 "+"grade2 "+"grade3 "+"average ");
();
//for (int i=0; i<; i++)
//(stud[i]);
for (int i=0; i<; i++) {
if(stud[i].() == 5) {
(stud[i].id+" ");
(stud[i].name+" ");
}else if(stud[i].() > 6) {
(stud[i].id+" ");
(stud[i].name+" ");
}else {
(stud[i].id+" ");
(stud[i].name+" ");
}
for (int j=0; j<stud[i].; j++) {
(stud[i].grade[j]+" ");
}
((stud[i].sum/3)+" ");
();
}
();
}
}
/*
2017年3月13日16:17:38
java基础50道经典练习题 例50
Athor: ZJY
Purpose:
*/
import .*;
public class ProgramNo50_2
{
//定义学生模型
String[] number = new String[5];
String[] name = new String[5];
float[][] grade = new float[5][3];
float[] sum = new float[5];
public static void main(String[] args) throws Exception{
ProgramNo50_2 stud = new ProgramNo50_2();
();
();
}
//输入学号、姓名、成绩
void input() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader());
//录入状态标识
boolean isRecord = true;
while(isRecord){
try{
for(int i=0; i<5; i++) {
("请输入学号:");
number[i] = ();
("请输入姓名:");
name[i] = ();
for(int j=0; j<3; j++) {
("请输入第"+(j+1)+"门课成绩:");
grade[i][j] = (());
}
();
sum[i] = grade[i][0]+grade[i][1]+grade[i][2];
}
isRecord = false;
}catch(NumberFormatException e){
("请输入一个数字!");
}
}
}
//输出文件
void output() throws IOException{
FileWriter fw = new FileWriter("G:/Project/java_object/java基础50道经典练习题/topic50/学生信息");
BufferedWriter bw = new BufferedWriter(fw);
("No. "+"Name "+"grade1 "+"grade2 "+"grade3 "+"average");
();
for(int i=0; i<5; i++) {
(number[i]);
(" "+name[i]);
for(int j=0; j<3; j++)
(" "+grade[i][j]);
(" "+(sum[i]/5));
();
}
();
}
}