今天在CSDN上面回答了一个小问题,是一个学生的作业。他自己的描述如下:
1.编写一个程序:要求从DOS控制台以提问对话的形式,输入三个同学的c语言成绩与java成绩,并将成绩保存到一个“chengji.txt”的文件中.
2.提示:1.创建一个Student学生类,其中域变量包括name,age,javaScore和cScore;
2.创建一个TestChengji类,在main主方法中分别创建三个对象数组:
Student []stu=new Student[3];
stu[] = new Student( );
String []javaScore=new String[3];
String []cScore=new String[3];
将控制台输入的数据保存在数组中,然后在保存到一个文件中。
3.根据上面的程序,编写一个程序,从文件chengji.txt中取出三个同学的成绩,并求出平均成绩输出到dos控制台上显示出来。
下面的代码是我写的,很不完善,缺少必要的验证机制,但是实现了他说的基本功能。
记录在这里,以后说不定有用呢。呵呵
/**
* <pre>
* @author kanpiaoxue
* Date 2013-11-27
* </pre>
*/
public class Student {
private String name;
private int age;
private double javaScore;
private double cScore;
public Student() {
super();
}
public Student(String name, int age, double javaScore, double cScore) {
super();
this.name = name;
this.age = age;
this.javaScore = javaScore;
this.cScore = cScore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getJavaScore() {
return javaScore;
}
public void setJavaScore(double javaScore) {
this.javaScore = javaScore;
}
public double getCScore() {
return cScore;
}
public void setCScore(double cScore) {
this.cScore = cScore;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import com.alibaba.fastjson.JSON;
/**
* <pre>
* @author kanpiaoxue
* Date 2013-11-27
* </pre>
*/
public class TestChengji {
private static final String DATA_FILE = "D:\\tmp\\students.txt";
/**
* <pre>
* 连续提问学生的姓名,年龄,java成绩,C成绩。当输入stop的时候,结束,并计算结果
* </pre>
*
* @throws Exception
*/
public static void main(String[] args) throws Exception {
TestChengji t = new TestChengji();
List<Student> students = t.createStudents();
t.saveStudentsToFile(students);
students = t.readStudentsFromFile();
t.calculateStudents(students);
/**
* <pre>
*
* 下面是程序的运行结果:
*
* Please enter student name :student1
* Please enter student age :20
* Please enter student java score :98
* Please enter student C score :89
* Do you continue enter student infomation? (yes/no) : yes
* Please enter student name :student2
* Please enter student age :20
* Please enter student java score :90
* Please enter student C score :88
* Do you continue enter student infomation? (yes/no) : yes
* Please enter student name :student3
* Please enter student age :20
* Please enter student java score :85
* Please enter student C score :85
* Do you continue enter student infomation? (yes/no) : no
* stop enter students infomation.
* write datas to D:\tmp\students.txt successfully!
* start reading students infomation from D:\tmp\students.txt
* 3 students java average score : 91.0, c average score : 87.33333333333333
*
* </pre>
*/
}
/**
* @param reader
* @return
* @throws Exception
* 创建学生。 这里呢,我没有给你校验学生的age,javaScore,cScore的合法性。你可以自己进行校验。
*/
private Student createStudent(BufferedReader reader) throws Exception {
System.out.print("Please enter student name :");
String str = null;
Student student = new Student();
str = reader.readLine();
student.setName(str);
System.out.print("Please enter student age :");
str = reader.readLine();
student.setAge(Integer.valueOf(str));
System.out.print("Please enter student java score :");
str = reader.readLine();
student.setJavaScore(Double.valueOf(str));
System.out.print("Please enter student C score :");
str = reader.readLine();
student.setCScore(Double.valueOf(str));
return student;
}
private List<Student> createStudents() {
List<Student> list = new ArrayList<Student>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(System.in));
Student student = createStudent(reader);
list.add(student);
System.out
.print("Do you continue enter student infomation? (yes/no) : ");
String str = reader.readLine();
if (str.trim().equalsIgnoreCase("no")
|| str.trim().equalsIgnoreCase("n")) {
System.out.println("stop enter students infomation.");
return list;
} else {
List<Student> l = createStudents();
list.addAll(l);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != reader) {
try {
reader.close();
} catch (IOException e) {
}
}
}
return list;
}
private void saveStudentsToFile(List<Student> students) throws Exception {
File f = new File(DATA_FILE);
f.delete();
f.createNewFile();
if (f.isFile()) {
FileWriter writer = new FileWriter(f);
for (Student s : students) {
String json = JSON.toJSONString(s);
writer.write(json + "\n");
}
writer.flush();
writer.close();
System.out.println("write datas to " + f + " successfully!");
} else {
throw new IllegalArgumentException("please check file " + f);
}
}
private List<Student> readStudentsFromFile() throws Exception {
File f = new File(DATA_FILE);
System.out.println("start reading students infomation from " + f);
@SuppressWarnings("unchecked")
List<String> lines = IOUtils.readLines(new FileInputStream(f));
List<Student> list = new ArrayList<Student>();
for (String str : lines) {
Student s = JSON.parseObject(str, Student.class);
list.add(s);
}
return list;
}
private void calculateStudents(List<Student> students) {
double javaSum = 0D;
double cSum = 0D;
for (Student s : students) {
javaSum += s.getJavaScore();
cSum += s.getCScore();
}
int size = students.size();
System.out.println(size + " students java average score : "
+ (javaSum / size) + ", c average score : " + (cSum / size));
}
}