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

開心每一天1111
TA貢獻1836條經驗 獲得超13個贊
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 要 調了 api 直接 升序排列,你 寫個類似 冒泡 排序的算法 排個序 就OK 了
添加回答
舉報
0/150
提交
取消