题目
1、生成0-100中的10个随机数,将10个随机数放入集合中,判断随机数是否为偶数,将所有偶数放到一个新的集合中,并显示偶数个数。
2、定义一个学生类Student,包含三个属性姓名、年龄、性别,创建五个学生对象存入集合中。要求学生类封装代码,输出所有学生信息,并按照年龄从大到小排序。
3、创建一个Computer类,属性为编号(即计算机的唯一标识)CPU型号,主板型号,硬盘大小,内存大小,显卡型号,价格;
使用集合存储计算机对象
为集合添加3个元素。
用户输入“编号”获取对应的Computer信息,并且输出具体的参数。
4、生成0-9的20个随机数,将20个随机数放入集合中,手动输入0-9的个位数,将集合中的出现的这个个位数放到集合最前面,优先输出。
5、生成0-100中的10个随机数,将10个随机数放入集合中,将整个集合倒置,放到新的集合中输出,即原集合第一位放到新集合最后一位。
注:生成随机数方法
Random random = new Random();
int num = random.nextInt(100);生成随机数0-100
解答
第一题: package homework1; import java.util.ArrayList; import java.util.List; public class Ran { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); List<Integer> store = new ArrayList<Integer>(); for (int i = 0; i < 10; i++) { int number = Double.valueOf(Math.random() * 100).intValue(); System.out.print(number+" "); list.add(number); } for(Integer n:list) { if(n%2==0) { store.add(n); } } System.out.println(); for (Integer s:store) { System.out.print(s+" "); } } } 第二题: package homwork2; import java.util.ArrayList; import java.util.Collections; public class TestStu { public static void main(String[] args) { ArrayList<Student> list=new ArrayList<Student>(); Student s1=new Student("张一", 12, "男"); Student s2=new Student("张二", 15, "男"); Student s3=new Student("张三", 19, "女"); Student s4=new Student("张四", 17, "男"); Student s5=new Student("张五", 21, "女"); list.add(s1); list.add(s2); list.add(s3); list.add(s4); list.add(s5); Collections.sort(list); for(Student s:list) { System.out.println(s); } } } package homwork2; public class Student implements Comparable<Student>{ private String name; private int age; private String sex; public Student(String name, int age, String sex) { super(); this.name = name; this.age = age; this.sex = sex; } /** * @return the name */ public final String getName() { return name; } /** * @param name the name to set */ public final void setName(String name) { this.name = name; } /** * @return the age */ public final int getAge() { return age; } /** * @param age the age to set */ public final void setAge(int age) { this.age = age; } /** * @return the sex */ public final String getSex() { return sex; } /** * @param sex the sex to set */ public final void setSex(String sex) { this.sex = sex; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]"; } @Override public int compareTo(Student o) { if(this.getAge()>o.getAge()) return -1; else if(this.getAge()>o.getAge()) return -1; else return 0; } } 第三题: package homwork3; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class TestComputer { public static void main(String[] args) { List<Computer> list=new ArrayList<Computer>(); list.add(new Computer("1", "CPU1", "主板1", 128, 256, "intel", 5600)); list.add(new Computer("2", "CPU2", "主板2", 256, 256, "intel", 7600)); list.add(new Computer("3", "CPU3", "主板3", 64, 128, "intel", 3600)); System.out.print("请输入id"); Scanner sc=new Scanner(System.in); String n=sc.next(); for(Computer c:list) { if(c.getId().equals(n)) { System.out.println(c); } } } } 第四题: package homework4; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Ran { public static void main(String[] args) { List<Integer> list=new ArrayList<Integer>(); for(int i=0;i<20;i++){ int number=Double.valueOf((Math.random()*9)).intValue(); list.add(number); System.out.print(number+" "); } System.out.print("输入数字:"); int num=new Scanner(System.in).nextInt(); for(int i=0;i<list.size();i++) { if(list.get(i)==num) { list.remove(i); list.add(0,num); } } System.out.println(list); } } 第五题: package homework5; import java.util.ArrayList; import java.util.List; public class Rand { public static void main(String[] args) { List<Integer> list=new ArrayList<Integer>(); List<Integer> newList=new ArrayList<Integer>(); for(int i=0;i<10;i++) { int num=Double.valueOf((Math.random()*100)).intValue(); list.add(num); System.out.print(num+" "); } for(Integer t:list) { newList.add(0,t); } System.out.println(); for(Integer tt:newList) { System.out.print(tt+" "); } } }