使用java.lang.Math類,生成50個0到99之間的不重復(fù)的隨機(jī)整數(shù),并按降序的順序輸出這些整數(shù)
2 回答

慕森王
TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超3個贊
List<Long> listlong = new ArrayList<Long>();
for (int i = 0; i < 50; i++) {
t:
while (true){
long temp = Math.round(Math.random()*100);
if(listlong.contains(temp)) continue t ;
else {
listlong.add(temp);
break ;
}
}
}
Collections.sort(listlong);
for(Long l : listlong){
System.out.println(l);
}
Collections sort 要 調(diào)了 api 直接 升序排列,你 寫個類似 冒泡 排序的算法 排個序 就OK 了
添加回答
舉報
0/150
提交
取消