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!