`
哈达f
  • 浏览: 119554 次
  • 性别: Icon_minigender_1
  • 来自: 广西
社区版块
存档分类
最新评论

一道题

    博客分类:
  • j2se
 
阅读更多
/*
有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括姓名,三门课成绩),
计算出总成绩,并把学生的信息和计算出的总分数高低顺序存放在磁盘文件"stu.txt"中。

zs,20,30,90
li,40,90,70
za,80,80,80
kk,60,90,80



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

	Student(String name,int cn,int en,int ma)
	{
		this.name = name;
		this.cn = cn;
		this.en = en;
		this.ma = ma;
		sum = cn+en+ma;
	}
	public int compareTo(Student s)
	{
		if(this.sum>s.sum)
			return 1;
		if(this.sum==s.sum)
			return this.name.compareTo(s.name);
		return -1;
	}
	public int getSum()
	{
		return sum;
	}
	public String getName()
	{
		return name;
	}
}


class InfoUtil
{
	public static Set<Student> getInfo()throws IOException
	{
		return getInfo(null);
	}

	/*
	获取键盘录入信息,并封装成学生对象,存入集合中。
	*/
	public static Set<Student> getInfo(Comparator<Student> com)throws IOException
	{
		BufferedReader bufr = 
			new BufferedReader(new InputStreamReader(System.in));

		String line = null;
		TreeSet<Student> ts =null;
		if(com==null)
			ts = new TreeSet<Student>();
		else
			ts = new TreeSet<Student>(com);

		while((line=bufr.readLine())!=null)
		{
			if("over".equals(line))
				break;

			String[] arr = line.split(",");

			ts.add(new Student(arr[0],Integer.parseInt(arr[1])
							,Integer.parseInt(arr[2])
							,Integer.parseInt(arr[3])));
		}
		bufr.close();

		return ts;
	}

	/*
	遍历集合,获取学生信息,并写入到文件中。
	*/
	public static void write2File(Set<Student> s)throws IOException
	{
		Iterator<Student> it = s.iterator();

		BufferedWriter bufw  =
			new BufferedWriter(new FileWriter("stu.txt"));

		while(it.hasNext())
		{
			Student stu = it.next();

			bufw.write(stu.getName()+"\t"+stu.getSum());
			bufw.newLine();
			bufw.flush();
		}
		bufw.close();


	}
}


class  StudentInfo
{
	public static void main(String[] args) throws IOException
	{
		Comparator<Student> com  = Collections.reverseOrder();
		Set<Student> s = InfoUtil.getInfo(com);
		InfoUtil.write2File(s);
	}
}

 

/*赌游戏
*/

import java.util.*;
class Die
{
	private int faceValue;
	public void rol()
	{
		Random r = new Random();
		faceValue = r.nextInt(6)+1;
	}
	public int getFaceValue()
	{
		return faceValue;
	}
}

class  DiceGame
{

	private Die die1,die2;

	public void play()
	{
		die1 = new Die();
		die2 = new Die();

		die1.rol();
		die2.rol();

		int x = die1.getFaceValue();
		int y = die2.getFaceValue();

		if((x+y)==7)
			System.out.println("中了,恭喜");
		else
			System.out.println("郁闷!!");

	}
	public static void main(String[] args) 
	{
		 new DiceGame().play();
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics