首页 > 其他 > 详细

随机获取10个1-20之间的随机数,输出不重复的不能重复

时间:2017-04-10 17:07:27      阅读:259      评论:0      收藏:0      [点我收藏+]
/*
 * 需求:随机获取10个1-20之间的随机数,输出不重复的不能重复
 */

public class Demo {
    public static void main(String[] args) {
        // 创建大集合
        ArrayList<Integer> array = new ArrayList<>();
        //产生10个10到20的随机数
        for(int x=0;x<10;x++){
            int y =(int) (Math.random()*10+10);
            //输出产生的数
            System.out.print(y+"\t");
            //判断没有就添加
            if(!array.contains(y)){
                array.add(y);
            }
        }
        System.out.println();
        Iterator<Integer> it = array.iterator();
        while(it.hasNext()){
            int a =it.next();
            System.out.print(a+"\t");
        }

    }
}

 

随机获取10个1-20之间的随机数,输出不重复的不能重复

原文:http://www.cnblogs.com/flei/p/6689436.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!