黑马程序员---IO和集合的小练习

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

 

有五个学生,每个学生有3门课的成绩,
从键盘输入以上数据(包括姓名,三门课成绩),
输入的格式:如:zhagnsan,30,40,60计算出总成绩,
并把学生的信息和计算出的总分数高低顺序存放在磁盘文件"stuInfo.txt"中。

 

java.lang
类 Integer

java.lang.Object

           |--java.lang.Number

                        |--java.lang.Integer

public final class Integer

extends Number implements Comparable< Integer>

static intparseInt(String s)
          将字符串参数作为有符号的十进制整数进行解析。
static intparseInt(String s, int radix)
          使用第二个参数指定的基数,将字符串参数解析为有符号的整数。

 

java.util
类 Collections

java.lang.Object

          |--java.util.Collections

public class Collections

extends Object

static
<T> Comparator<T>
reverseOrder()
          返回一个比较器,它强行逆转实现了 Comparable 接口的对象 collection 的自然顺序

因为TreeSet集合默认排序是按从小到大的顺序排序的。

我们的学生类要求按学生成绩的从大到小排。

所以用到以上反序方法。

/*
有五个学生,每个学生有3门课的成绩,
从键盘输入以上数据(包括姓名,三门课成绩),
输入的格式:如:zhagnsan,30,40,60计算出总成绩,
并把学生的信息和计算出的总分数高低顺序存放在磁盘文件"stud.txt"中。

1,描述学生对象。
2,定义一个可操作学生对象的工具类。

思想:
1,通过获取键盘录入一行数据,并将该行中的信息取出封装成学生对象。
2,因为学生有很多,那么就需要存储,使用到集合。因为要对学生的总分排序。
    所以可以使用TreeSet。
3,将集合的信息写入到一个文件中。
*/

import java.io.*;
import java.util.*;
class Student implements Comparable<Student>
{
	private String name;
	private int math,cn,english;
	private int sum;

	Student(String name, int math, int cn, int english)
	{
		this.name = name;
		this.math = math;
		this.cn = cn;
		this.english = english;
		sum = math+cn+english;
	}
	public String getName()
	{
		return name;
	}
	public int getSum()
	{
		return sum;
	}
	public String toString()
	{
		return "Student["+name+","+math+","+cn+","+english+"]";
	}

	public int hashCode()
	{
		return name.hashCode()+sum*39;
	}
	public boolean equals(Object obj)
	{
		if(!(obj instanceof Student))
		{
			throw new RuntimeException("obj不是Student的实例");
		}
		Student s = (Student)obj;
		return this.name.equals(s.name) && this.sum==s.sum;
	}

	public int compareTo(Student stu)
	{
		int num = new Integer(this.sum).compareTo(new Integer(stu.sum));
		if(0==num)
		{
			return this.name.compareTo(stu.name);
		}
		return num;
	}
}

class StudentInfoTools
{
	public static TreeSet<Student> getStudents()throws IOException
	{
		return getStudents(null);
	}
	public static TreeSet<Student> getStudents(Comparator<Student> cmp)throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = null;
		TreeSet<Student> ts = new TreeSet<Student>(cmp);
		while((line=br.readLine())!=null)
		{
			if(line.equals("over"))
			{
				break;
			}
			String[] stus = line.split(",");
			Student stu = new Student(stus[0], Integer.parseInt(stus[1]),Integer.parseInt(stus[2]),Integer.parseInt(stus[3]));
			ts.add(stu);
		}
		br.close();
		return ts;
	}
	public static void writeFile(Set<Student> ts)throws IOException
	{
		BufferedWriter bw = new BufferedWriter(new FileWriter("stuInfo.txt"));
		for(Student s : ts)
		{
			bw.write(s.toString()+"\t");
			bw.write(s.getSum()+"");
			bw.newLine();
			bw.flush();
		}
		bw.close();
	}
}

class StudentInfoTest 
{
	public static void main(String[] args) throws IOException
	{
		Comparator<Student> cmp = Collections.reverseOrder();
		TreeSet<Student> ts = StudentInfoTools.getStudents(cmp);
		StudentInfoTools.writeFile(ts);
	}
}


 

 ---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值