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