刚学输出输入流~老师给的题目不会做。。。求指教
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));
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;
import javax.imageio.stream.FileCacheImageInputStream;
public class TestChengji {
public static void main(String[] args) throws IOException {
Student[] students=new Student[3];
String[] javaScore=new String[3];
String[] cScore =new String[3];
int i=0;
System.out.println("请输入第一位学生的java成绩,c成绩,以空格分隔,结束先按回车再输入no");
//获得键盘输入
Scanner sc=new Scanner(System.in);
//当你输入1空格2回车 然后输入no就结束
while(!sc.hasNext("no"))
{
javaScore[i]=sc.next();
cScore[i]=sc.next();
i++;
System.out.println("请输入第"+(i+1)+"位学生的java成绩,c成绩,以空格分隔");
}
//获得文件字节流
FileOutputStream out=new FileOutputStream("E:\\chengji.txt");
//字节流与字符流的桥梁
OutputStreamWriter ow=new OutputStreamWriter(out);
//将字节流转化为带buffer的字符流
BufferedWriter bw=new BufferedWriter(ow);
//将成绩输如文件 以逗号分割
for(int j=0;j<i;j++)
{
bw.write(javaScore[j]+","+cScore[j]);
bw.newLine();
}
//close资源,才会最终写入文件,否者不会写入
bw.close();
ow.close();
out.close();
//将下面的注释解开就是读取了
//
//chengjiOut c=new chengjiOut();
//c.getPingJunScore();
}
}
class chengjiOut
{
public void getPingJunScore() throws IOException
{
//得到文件的字节流
FileInputStream in=new FileInputStream("E:\\chengji.txt");
//inputStreamReader 字节流转换为字符流的桥梁
InputStreamReader ir=new InputStreamReader(in);
//有buffer的字符流
BufferedReader br=new BufferedReader(ir);
int s=0;//计算行数
String line="";//读取一行
String[] dst = null;//读取一行并将内容放入dst数组
int sum[][]=new int[3][1];//存放学生数据
//读取一行
while((line=br.readLine())!=null)
{
s++;
dst=line.split(",");
if(s==1)
{
for(int j=0;j<dst.length;j++)
{
sum[0][0]=sum[0][0]+Integer.valueOf(String.valueOf(dst[j]));
}
}
if(s==2)
{
for(int j=0;j<dst.length;j++)
{
sum[1][0]=sum[1][0]+Integer.valueOf(String.valueOf(dst[j]));
}
}
if(s==3)
{
for(int j=0;j<dst.length;j++)
{
sum[2][0]=sum[2][0]+Integer.valueOf(String.valueOf(dst[j]));
}
}
}
br.close();
ir.close();
in.close();
System.out.println("第一位学生平均成绩:"+((float)sum[0][0])/2);
System.out.println("第二位学生平均成绩:"+((float)sum[1][0])/2);
System.out.println("第三位学生平均成绩:"+((float)sum[2][0])/2);
}
}
class Student
{
private String name;
private int age;
private String cScore;
private String javaScore;
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 String getcScore() {
return cScore;
}
public void setcScore(String cScore) {
this.cScore = cScore;
}
public String getJavaScore() {
return javaScore;
}
public void setJavaScore(String javaScore) {
this.javaScore = javaScore;
}
}