黑马程序员_java输出输入流练习题

时间:2021-11-11 11:52:36
---------------------- android培训java培训、期待与您交流! ----------------------

题目:编写一个程序, 可以从键盘输入读取多个学生成绩, 输入quit时退出.程序将输入数据写入stu.txt文件(UTF-8), 文件中按照学生成绩总分排序.       

思路: 1、读取键盘输入?

                   读键盘用System.in,但是读的是字节,所以需要转成InputStreamReader来读字符,由于成绩是一行一行输入,所以需要转成BufferedReader来读取一行

                   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

             2、 题目要求读取多个学生成绩,并且输入quit时退出  

                    所以需要用到循环,不确定循环次数,所以用while,什么时候循环结束?当输入quit时结束。用"quit".equals("读取的内容")即可

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
String line = br.readLine();
if("quit".equals(line))
break;
}

              3、要把读到到输入记录下来

                    每一行输入的内容大概是: 姓名,77,88,55  。题目要求读取后还按照原格式存储,并且按成绩总分排序。可以把这些数据封装成一个对象,写一个Student类

                    属性:private String name; private int chinese; private int math; private int english; private int sum;

                    构造函数:

public Student(String name, int chinese, int math, int english) {
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
this.sum = chinese + math + english;
}

              4、存入到什么容器

                     每一行数据是一个对象,并且存入多个对象,并且按照总分排序,所以用TreeSet

                     存入TreeSet会自动排序,但是存入的是对象,所以需要在Student类上实现Comparable接口,实现compareTo方法         

public int compareTo(Student o) {
int sumGap = this.sum - o.sum;
return sumGap != 0 ? sumGap : this.name.compareTo(o.name);
}

              5、再把TressSet的数据写出到文件里

Student类代码:

public class Student implements Comparable<Student> {
private String name;
private int chinese;
private int math;
private int english;
private int sum;

public Student(String name, int chinese, int math, int english ) {
super();
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
this.sum = chinese + math + english;
}
public int compareTo(Student o) {
return o.sum - this.sum;
}
public String toString() {
return name + "," + chinese + "," + math + "," + english + "," + sum;
}
}

主程序类代码:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.TreeSet;
public class Exercise3 {
public static void main(String[] args) throws IOException {
TreeSet<Student> set = new TreeSet<Student>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
String line = br.readLine();
if("quit".equals(line))
break;
String[] arr = line.split(",");
Student stu = new Student(arr[0],Integer.parseInt(arr[1]),Integer.parseInt(arr[2]),Integer.parseInt(arr[3]));
set.add(stu);
}
OutputStreamWriter osw =new OutputStreamWriter(new FileOutputStream("stu.txt",true));
for (Student student : set) {
osw.write(student.toString());
}
osw.close();
}

private static void demo() throws IOException, FileNotFoundException {
TreeSet<Student> set = new TreeSet<Student>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
String line = br.readLine();
if("quit".equals(line))
break;
String[] arr = line.split(",");
Student stu = new Student(arr[0], Integer.parseInt(arr[1]), Integer.parseInt(arr[2]), Integer.parseInt(arr[3]));
set.add(stu);
}
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("stu.txt",true));
for (Student student : set) {
osw.write(student.toString()+"\r\n");
}
osw.close();
}
}

---------------------- android培训java培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net/heima