有沒有辦法使得如果randNumber的輸出低於75,那麼randNumber的輸出將變成比85更低的數字?如果隨機數發生器選擇75作爲第一個數字,那麼有辦法將之後的其餘輸出值改爲低於85(它現在設置的值)。如何創建隨Java中for循環中每個x值更改的隨機整數?
public static void Hypothetical() {
int x;
for (x = 0; x <= 4; x++) {
Random z = new Random();
int zNumber = z.nextInt(10-5) + 5;
Random rand = new Random();
int randNumber = rand.nextInt(85-65) + 65;
if (x == 0) {
System.out.println("You may begin running.");
//TimeUnit.SECONDS.(2);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (x == 1) {
System.out.println("Your first lap was made in " + randNumber + " seconds");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (x==2) {
System.out.println("Your second lap was made in " + (randNumber + zNumber) + " seconds");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (x==3) {
System.out.println("Your third lap was made in " + (randNumber + zNumber) + " seconds");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (x==4) {
System.out.println("Your final lap was made in " + (randNumber + zNumber) + " seconds");
}
}
}
}
你就不能再生/重新分配zNumber和randNumber內循環? –
你真的需要每次都有不同的號碼,或者如果他們大多數時間都不一樣,它可以嗎?如果你要使用'rand.nextInt(20)'生成一個隨機數序列,那麼任何數字都會與前一個數字相同的概率爲1/20。如果你想確保它們完全不同,你需要一個不同的算法。 – ajb
請不要通過編輯完全重寫這樣的問題,因爲它會讓別人寫下的評論和回答看起來很愚蠢。最好刪除並提出一個新問題。但是,你仍然不清楚你想完成什麼。 – ajb