本人菜鸟 寻求答案
Java实践幸运观众问题假设某班级有20个同学(姓名无重名) 参加某个晚会,晚会上随机抽出5名幸运观众(程序结果显示出幸运观众的名字)
请问大神这该怎么编程序。= = 求过程
谢谢帮忙!
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class 幸运观众 { public static void main(String[] args) { String firstName[]={"赵","钱","孙","李","周","吴","郑","王","冯","陈","楮","卫","蒋","沈","韩","杨","朱","秦","尤","许"}; String lastName[]={"三","四","五","六","七","八","九","伟","楠","理","平","好","富","故","佳","家","云","逢","风","嘻"}; Person p[]=new Person[20]; List list1=Arrays.asList(firstName); List list2=Arrays.asList(lastName); ArrayList arraylist1=new ArrayList(list1); ArrayList arraylist2=new ArrayList(list2); for (int i = 0; i < p.length;) { int a=((int)(Math.random()*100))%(list1.size()); int b=((int)(Math.random()*100))%(list2.size()); if(a>=arraylist1.size()||b>=arraylist2.size()) continue; p[i]=new Person(); p[i].setId(i); p[i].setName(arraylist1.get(a).toString()+arraylist2.get(b).toString()); arraylist1.remove(a); arraylist2.remove(b); i++; } List<Integer> alist=new ArrayList<Integer>(); for(int i=0;i<5;){ int a=((int)(Math.random()*100))%20; if(!alist.contains(a)&&a!=0){ alist.add(a); i++; } } System.out.println("幸运观众:"); for (int i = 0; i < 5; i++) { System.out.println(p[ alist.get(i)].getId()+"号\t"+p[alist.get(i)].getName()+"\t成为幸运观众"); } } } class Person{ private String name; private int id; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class game { public static void main(String[] args) { String firstName[]={"赵","钱","孙","李","周","吴","郑","王","冯","陈","楮","卫","蒋","沈","韩","杨","朱","秦","尤","许"}; String lastName[]={"三","四","五","六","七","八","九","伟","楠","理","平","好","富","故","佳","家","云","逢","风","嘻"}; Person p[]=new Person[20]; List<String> list1=Arrays.asList(firstName); List<String> list2=Arrays.asList(lastName); ArrayList<String> arraylist1=new ArrayList<String>(list1); ArrayList<String> arraylist2=new ArrayList<String>(list2); for (int i = 0; i < p.length;i++) { int a=((int)(Math.random()*100))%(list1.size()); int b=((int)(Math.random()*100))%(list2.size()); if(a>=arraylist1.size()||b>=arraylist2.size()) continue; String names=arraylist1.get(a).toString().trim()+arraylist2.get(b).toString().trim(); for(int j=0;j<i;j++) if(p[j].getName().equals(names)){ i--; continue; } p[i]=new Person(); p[i].setId(i); p[i].setName(names); } List<Integer> alist=new ArrayList<Integer>(); for(int i=0;i<5;i++){ int a=((int)(Math.random()*100))%20; if(!alist.contains(a)&&a!=0){ alist.add(a); } else i--; } System.out.println("幸运观众:"); for (int i = 0; i < 5; i++) { System.out.println(p[alist.get(i)].getId()+"号\t"+p[alist.get(i)].getName()+"\t成为幸运观众"); } } } class Person{ private String name; private int id; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } }