隨機獲取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