課程
/后端開發(fā)
/Java
/Java入門第三季
如何生成3個不重復(fù)的1000以內(nèi)的隨機(jī)正整數(shù)作為學(xué)生Id,大家有沒有好的建議?。?/p>
2015-06-02
源自:Java入門第三季 6-8
正在回答
public void testSort4(){
List<Student> studentList=new ArrayList<Student>();
List<Integer> randomList=new ArrayList<Integer>();
Random random=new Random();
int k;
for(int i=0;i<3;i++){
do{
k=random.nextInt(1000);
}while(randomList.contains(k));
randomList.add(k);
}
studentList.add(new Student(randomList.get(0)+"","luci"));
studentList.add(new Student(randomList.get(1)+"","nibi"));
studentList.add(new Student(randomList.get(2)+"","good"));
System.out.println("*****排序前******");
for(Student stu:studentList){
System.out.println("學(xué)生"+stu.name );
System.out.println("*****排序后*********");
Collections.sort(studentList);
System.out.println("學(xué)生"+stu.name+" "+stu.id);
//用new StudentComparator()來接收return的值。
Collections.sort(studentList,new StudentComparator());
System.out.println("****根據(jù)姓名排序*******");
System.out.println("根據(jù)姓名:"+stu.id+" "+stu.name);
北海糖
hash表判斷重復(fù)。。。
3個不重復(fù)的1000以內(nèi)正整數(shù),保存在數(shù)組里然后輸出出來:
public static void main(String[] args){ ? ? Random r=new Random(); ? ? int[] s=new int[3]; ? ? Boolean flag=true; ? ? ? ? s[0]=r.nextInt(1000); ? ? int i=1; ? ? while(flag&&i<3) ? ? { ? ? ?flag=false; ? ? ?s[i]=r.nextInt(1000); //產(chǎn)生0到100的隨機(jī)數(shù) ? ? ?if(s[i]==s[i-1]) ? ? ? break; ? ? ?else ? ? ?{ ? ? ? i++; ? ? ? flag=true; ? ? ?} ? ? } ? ? for(int j=0;j<3;j++) ? ? ? ? System.out.println(s[j]);? ? }
天藍(lán)色的彼岸_123 提問者
Nick_arron
循環(huán)生成3次,每生成一次和前一次的判斷一次,重復(fù)的跳過。
舉報
Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧
1 回答【6-8完善練習(xí)】生成3個不重復(fù)的1000以內(nèi)的隨機(jī)正整數(shù)作為學(xué)生Id
4 回答生成3個不重復(fù)的1000以內(nèi)的隨機(jī)正整數(shù)作為學(xué)生ID的程序,請多多指教?。?!
6 回答生成1000以內(nèi)不重復(fù)的隨機(jī)數(shù)
6 回答完善隨機(jī)生成不重復(fù)學(xué)生ID并排序
3 回答隨機(jī)生成不重復(fù)的字符串
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2015-07-27
public void testSort4(){
List<Student> studentList=new ArrayList<Student>();
List<Integer> randomList=new ArrayList<Integer>();
Random random=new Random();
int k;
for(int i=0;i<3;i++){
do{
k=random.nextInt(1000);
}while(randomList.contains(k));
randomList.add(k);
}
studentList.add(new Student(randomList.get(0)+"","luci"));
studentList.add(new Student(randomList.get(1)+"","nibi"));
studentList.add(new Student(randomList.get(2)+"","good"));
System.out.println("*****排序前******");
for(Student stu:studentList){
System.out.println("學(xué)生"+stu.name );
}
System.out.println("*****排序后*********");
Collections.sort(studentList);
for(Student stu:studentList){
System.out.println("學(xué)生"+stu.name+" "+stu.id);
}
//用new StudentComparator()來接收return的值。
Collections.sort(studentList,new StudentComparator());
System.out.println("****根據(jù)姓名排序*******");
for(Student stu:studentList){
System.out.println("根據(jù)姓名:"+stu.id+" "+stu.name);
}
}
2015-06-02
hash表判斷重復(fù)。。。
2015-06-02
3個不重復(fù)的1000以內(nèi)正整數(shù),保存在數(shù)組里然后輸出出來:
public static void main(String[] args){
? ? Random r=new Random();
? ? int[] s=new int[3];
? ? Boolean flag=true;
? ?
? ? s[0]=r.nextInt(1000);
? ? int i=1;
? ? while(flag&&i<3)
? ? {
? ? ?flag=false;
? ? ?s[i]=r.nextInt(1000); //產(chǎn)生0到100的隨機(jī)數(shù)
? ? ?if(s[i]==s[i-1])
? ? ? break;
? ? ?else
? ? ?{
? ? ? i++;
? ? ? flag=true;
? ? ?}
? ? }
? ? for(int j=0;j<3;j++)
? ? ? ? System.out.println(s[j]);
? ? }
2015-06-02
循環(huán)生成3次,每生成一次和前一次的判斷一次,重復(fù)的跳過。