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


/*
 * 需求:随机获取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");
        }

    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM