5.10作业

1)键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台
  a:自然排序
  b:选择器排序

public static void main(String[]args) {
		
		//创建集合
		TreeSet<student> ts=new TreeSet<student>();
		
		
		for(int i=0;i<5;i++) {
			System.out.println("请输入第"+(i+1)+"个学生的姓名及成绩");
			Scanner sc=new Scanner(System.in);
			//键盘录入数据
			String name=sc.nextLine();
			int chinese=sc.nextInt();
			int math=sc.nextInt();
			int english=sc.nextInt();
			
			//创建学生对象
			student s=new student();
			//将数据封装到学生对象中
			s.setName(name);
			s.setChinese(chinese);
			s.setMath(math);
			s.setEnglish(english);
			
			//将学生对象添加到集合中
			ts.add(s);
		}
		
		for(student s:ts) {
			System.out.println(s.getName()+" "+s.getChinese()+" "+s.getMath()+" "+s.getEnglish());
		}
	}
	

//student类

public class student implements Comparable<student>{

	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public student(String name) {
		super();
		this.name = name;
	}
	private int chinese;
	private int math;
	private int english;
	public student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public student(int chinese, int math, int english) {
		super();
		this.chinese = chinese;
		this.math = math;
		this.english = english;
	}
	public int getChinese() {
		return chinese;
	}
	public int getEnglish() {
		return english;
	}
	public int getMath() {
		return math;
	}
	public void setChinese(int chinese) {
		this.chinese = chinese;
	}
	public void setEnglish(int english) {
		this.english = english;
	}
	public void setMath(int math) {
		this.math = math;
	}
	
	public int compareTo(student s) {
		int num=s.getChinese()+s.getMath()+s.getEnglish()-this.getChinese()-this.getMath()-this.getEnglish();
		return num;
	}
	
	
}


2)获取10随机数,1-20之间,要求值不能重复!(ArrayList,HashSet)

public class test {

	public static void main(String[]args) {
		//创建集合
		Set<Integer> set=new HashSet<Integer>();
		
		//创建Random类对象
		Random r=new Random();
		
		while(set.size()<10) {
			//获取随机数
			int num=r.nextInt(20)+1;
			set.add(num);
		}
		
		
			System.out.println(set);
		
	}
}


3)ArrayList集合嵌套HashMap集合并遍历。
  需求:
  假设ArrayList集合的元素是HashMap。有3个。
  每一个HashMap集合的键和值都是字符串。
  元素我已经完成,请遍历。
 周瑜---小乔
 吕布---貂蝉
 郭靖---黄蓉
 杨过---小龙女
 令狐冲---任盈盈
 林平之---岳灵珊
ArrayList<HashMap<String,String>>



public class test {

	public static void main(String[]args) {
		//创建一个大集合
		ArrayList<HashMap<String,String>> bigList=new ArrayList<HashMap<String,String>>();
		
		//创建第一个集合
		Map<String,String> FirstList=new HashMap<String,String>();
		//给第一个集合添加元素
		FirstList.put("周瑜", "小乔");
		FirstList.put("吕布", "貂蝉");
		//把第一个集合添加到大集合中
		bigList.add((HashMap<String, String>) FirstList);
		
		//创建第二个集合
		Map<String,String> SecondList=new HashMap<String,String>();
		//给第二个集合添加元素
		SecondList.put(" 郭靖", "黄蓉");
		SecondList.put("林平之", "岳灵珊");
		//把第二个集合添加到大集合中
		bigList.add((HashMap<String, String>) SecondList);
		
		//创建第三个集合
		Map<String,String> ThirdList=new HashMap<String,String>();
		//给第三个集合添加元素
		ThirdList.put("令狐冲", "任盈盈");
		ThirdList.put("杨过", "小龙女");
		//把第三个集合添加到大集合中
		bigList.add((HashMap<String, String>) ThirdList);
		
		
		// 遍历集合
		for (HashMap<String, String> hm : bigList) {
			Set<String> set = hm.keySet();
			for (String key : set) {
				String value = hm.get(key);
			System.out.println(key + "---" + value);
			}
		}
		
	
	}
}

猜你喜欢

转载自blog.csdn.net/fnwibwj/article/details/80268051