黑马程序员技术交流社区

标题: 代码报错,求解 long round = Math.round(Math.random())咋用?? [打印本页]

作者: liujian5461267    时间: 2016-4-30 18:09
标题: 代码报错,求解 long round = Math.round(Math.random())咋用??

  1. import java.util.Comparator;
  2. import java.util.Random;
  3. import java.util.TreeSet;

  4. //产生十个1-20的随机数,从大到小排序,不能重复
  5. public class RandomDamo {
  6. public static void main(String[] args) {
  7.          
  8.         Random random = new Random();
  9.         TreeSet<Integer> ts= new TreeSet<Integer>(new Comparator<Integer>() {

  10.                 @Override
  11.                 public int compare(Integer a1, Integer a2) {
  12.                        
  13.                         return a2-a1;
  14.                 }
  15.         });
  16.         int count=0;
  17.         while(count<10) {
  18.         long round = Math.round(Math.random());
  19.         int i= (int)(round*20)+1;
  20.                 //int  i= random.nextInt(20);
  21.                 if (!ts.contains(i)) {
  22.                         ts.add( i);
  23.                         count++;
  24.                 }
  25.         }
  26.        
  27.         for (Integer integer : ts) {
  28.                 System.out.print(integer+", ");
  29.         }
  30. }
  31. }
复制代码

作者: liujian5461267    时间: 2016-4-30 19:31
整个论坛都没有人,哎
作者: 东方乔恩    时间: 2016-4-30 20:57
大神的代码,看不懂,还没有学到啊
作者: huaihkiss    时间: 2016-4-30 21:05
Round是什么
作者: liujian5461267    时间: 2016-5-1 08:07
math.random()能不能实现随机数啊??
作者: WDzyll    时间: 2016-5-1 08:58
liujian5461267 发表于 2016-5-1 08:07
math.random()能不能实现随机数啊??

我记得好像可以的




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2