課程
/后端開發
/Java
/Java入門第三季
如何生成3個不重復的1000以內的隨機正整數作為學生Id,大家有沒有好的建議啊?
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("學生"+stu.name );
System.out.println("*****排序后*********");
Collections.sort(studentList);
System.out.println("學生"+stu.name+" "+stu.id);
//用new StudentComparator()來接收return的值。
Collections.sort(studentList,new StudentComparator());
System.out.println("****根據姓名排序*******");
System.out.println("根據姓名:"+stu.id+" "+stu.name);
北海糖
hash表判斷重復。。。
3個不重復的1000以內正整數,保存在數組里然后輸出出來:
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); //產生0到100的隨機數 ? ? ?if(s[i]==s[i-1]) ? ? ? break; ? ? ?else ? ? ?{ ? ? ? i++; ? ? ? flag=true; ? ? ?} ? ? } ? ? for(int j=0;j<3;j++) ? ? ? ? System.out.println(s[j]);? ? }
天藍色的彼岸_123 提問者
Nick_arron
循環生成3次,每生成一次和前一次的判斷一次,重復的跳過。
舉報
Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧
1 回答【6-8完善練習】生成3個不重復的1000以內的隨機正整數作為學生Id
4 回答生成3個不重復的1000以內的隨機正整數作為學生ID的程序,請多多指教?。?!
6 回答生成1000以內不重復的隨機數
6 回答完善隨機生成不重復學生ID并排序
3 回答隨機生成不重復的字符串
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
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("學生"+stu.name );
}
System.out.println("*****排序后*********");
Collections.sort(studentList);
for(Student stu:studentList){
System.out.println("學生"+stu.name+" "+stu.id);
}
//用new StudentComparator()來接收return的值。
Collections.sort(studentList,new StudentComparator());
System.out.println("****根據姓名排序*******");
for(Student stu:studentList){
System.out.println("根據姓名:"+stu.id+" "+stu.name);
}
}
2015-06-02
hash表判斷重復。。。
2015-06-02
3個不重復的1000以內正整數,保存在數組里然后輸出出來:
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); //產生0到100的隨機數
? ? ?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
循環生成3次,每生成一次和前一次的判斷一次,重復的跳過。