黑马程序员技术交流社区
标题:
毕姥爷的视频照着写的就是不行咋办
[打印本页]
作者:
wangcongwu
时间:
2014-12-18 01:21
标题:
毕姥爷的视频照着写的就是不行咋办
IO 最后一个视频五个学生输入成绩然后打印到一个文件夹里,我按照毕姥爷的视频做的就是不行
ackage 黑马练习题五个学生三门课排序后放入Txt;
import java.io.*;
import java.util.*;
public class 键盘输入学生五个成绩三门排序后放文件中 {
public static void main(String[] args) throws IOException {
Set<Student> stus = StudentInfoTool.getStudents();
StudentInfoTool.write2File(stus);
Iterator<Student> it = stus.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class StudentInfoTool
{
public static Set<Student> getStudents() throws IOException
{
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line= null ;
TreeSet<Student> stus = new TreeSet<Student>();
while((line = bufr.readLine())!= null )
{
if("over".equals(line))
break;
String[] info = line.split(",");
Student stu = new Student(info[0],
Integer.parseInt(info[1]),
Integer.parseInt(info[2]),
Integer.parseInt(info[3]));
stus.add(stu);
}
bufr.close();
return stus;
}
public static void write2File (Set<Student> stus) throws IOException
{
BufferedWriter bufw = new BufferedWriter(new FileWriter("Student.txt"));
for(Student stu : stus )
{
bufw.write(stu.toString()+"\t");
System.out.println("w2f under \t");
bufw.write(stu.getSum());
System.out.println("fd");
bufw.newLine();
bufw.flush();
}
bufw.close();
}
}
class Student implements Comparable
{
private String name;
private int ma,cn,en;
private int sum;
Student(String name,int ma,int cn,int en)
{
this.name = name;
this.ma = ma;
this.en =en;
this.cn = cn;
sum = ma+cn+en;
}
public int compareTo(Student s)
{
int num = new Integer(this.sum).compareTo(new Integer(s.sum));
if(num == 0)
return this.name.compareTo(s.name);
return num;
}
public int compareTo(Object o) {
return 0;
}
public String getName()
{
return name;
}
public int getSum()
{
return sum;
}
public int hashCode()
{
return name.hashCode()+sum*78;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s = (Student) obj;
return this.name.equals(s.name) && this.sum == s.sum;
}
public String toString()
{
return "student["+name+","+cn+","+en+","+ma+"]";
}
}
复制代码
作者:
wangcongwu
时间:
2014-12-18 01:23
我去弄出来TreeSet 看也就是第一个学生能够进去,第二个都不行
作者:
wangcongwu
时间:
2014-12-18 01:27
网上的别的方法很多,但是我觉得毕姥爷的是最面向对象的感觉,但是就是不知道为啥我这个不行。
作者:
wangcongwu
时间:
2014-12-18 03:15
我自己写的Student 类就完全可以比较也能进去二叉树,为啥毕姥爷的不行呢,我把我写的和毕姥爷的放在一起大神们来比较下,难道说我比毕姥爷水平高???
package 黑马练习题五个学生三门课排序后放入Txt;
import java.io.*;
import java.util.*;
public class 键盘输入学生五个成绩三门排序后放文件中 {
public static void main(String[] args) throws IOException {
Set<Student> stus = StudentInfoTool.getStudents();
StudentInfoTool.write2File(stus);
Iterator<Student> it = stus.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class StudentInfoTool
{
public static Set<Student> getStudents() throws IOException
{
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line= null ;
TreeSet<Student> stus = new TreeSet<Student>();
while((line = bufr.readLine())!= null )
{
if("over".equals(line))
break;
String[] info = line.split(",");
for(int i = 0;i<info.length;i++)
{
System.out.println("info:"+info[i]);
}
Student stu = new Student(info[0],
Integer.parseInt(info[1]),
Integer.parseInt(info[2]),
Integer.parseInt(info[3]));
stus.add(stu);
}
bufr.close();
return stus;
}
public static void write2File (Set<Student> stus) throws IOException
{
BufferedWriter bufw = new BufferedWriter(new FileWriter("Student.txt"));
bufw.write("name"+"\t"+"cn"+"\t"+"en"+"\t"+"ma"+"\t"+"sum");
bufw.newLine();
for(Student stu : stus )
{
bufw.write(stu.toString()+"\t");
System.out.println("w2f under \t");
System.out.println("fd");
bufw.newLine();
bufw.flush();
}
bufw.close();
}
}
//俺自己的
class Student implements Comparable
{
String name;
int ma;
int cn;
int en;
int sum;
Student(String name,int ma,int cn,int en)
{
this.name = name;
this.ma = ma;
this.cn= cn;
this.en=en;
this.sum = ma+cn+en;
}
public String getName()
{
return this.name;
}
public int getMa()
{
return this.ma;
}
public int getSum()
{
return sum;
}
public String toString()
{
return name+"\t"+cn+"\t"+en+"\t"+ma+"\t"+sum;
}
public int compareTo(Object obj) {
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s = (Student)obj;
// System.out.println(this.name+"...compare to ..."+s.name);
if(this.sum>s.sum )
return -1 ;
if(this.sum == s.sum )
{
return this.name.compareTo(s.name);
}
return 1;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s = (Student) obj;
return this.name.equals(s.name) && this.sum == s.sum;
}
}
//毕姥爷的
//class Student implements Comparable
//{
// private String name;
// private int ma,cn,en;
// private int sum;
//
// Student(String name,int ma,int cn,int en)
// {
// this.name = name;
// this.ma = ma;
// this.en =en;
// this.cn = cn;
// sum = ma+cn+en;
// }
//
// public int compareTo(Student s)
// {
// int num = new Integer(this.sum).compareTo(new Integer(s.sum));
// if(num == 0)
// return this.name.compareTo(s.name);
// return num;
// }
//
// public int compareTo(Object o) {
// return 0;
// }
// public String getName()
// {
// return name;
//
// }
// public int getSum()
// {
// return sum;
// }
// public int hashCode()
// {
// return name.hashCode()+sum*78;
// }
// public boolean equals(Object obj)
// {
// if(!(obj instanceof Student))
// throw new ClassCastException("类型不匹配");
// Student s = (Student) obj;
// return this.name.equals(s.name) && this.sum == s.sum;
//
// }
// public String toString()
// {
// return "student["+name+","+cn+","+en+","+ma+"]";
// }
//}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2