簡體   English   中英

如何生成一個數組,其中設置了前兩個數字並且 rest 是隨機的?

[英]How do I produce an array where the first two numbers are set and the rest are random?

我需要制作一個包含 28 個隨機數和另外 2 個隨機數的數組:1 和 -100。 接下來我能夠生成 20 到 100 之間的隨機數; 但是,我不知道如何將 [0] 和 [1] 設置為 1 和 -100。

    for ( int i = 2 ; i < prizeBoxes.length ; i++) {
        
        prizeBoxes[i] = (int) Math.floor(Math.random()*(max-min) +min);

    }
public void initializeBoxes () {

prizeBoxes[0] = 1;
prizeBoxes[1] = -100;

    int min = 20;
    int max = 100;  

for ( int i = 2 ; i < prizeBoxes.length ; i++) {
        
    
     prizeBoxes[i] = (int) Math.floor(Math.random()*(max-min) +min);

    }

for ( int i=0; i < prizeBoxes.length ; i++){
System.out.println(prizeBoxes[i]) ;

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM