我想在指定的數字列表中生成兩個數字。我寫了這段代碼,但它不起作用。任何人都可以提出一個更好的解從指定的數字列表中生成兩個數字
public classl {
public static void main(String[] args) {
int[] lottery = new int[2];
int randomNum;
for (int i = 0; i < 3; i++) {
randomNum = (int) (Math.random() * (3,9,25,41,43,50,68)); // Random number created here.
for (int x = 0; x < i; x++) {
if (lottery[x] == randomNum) // Here, code checks if same random number generated before.
{
randomNum = (int) (Math.random() * (3,9,25,41,43,50,68));// If random number is same, another number generated.
x = -1; // restart the loop
}
}
lottery[i] = randomNum;
}
for (int i = 0; i < lottery.length; i++)
System.out.print(lottery[i] + " ");
}
}
}
爲什麼在'Math.random()*'後面有逗號分隔的數字列表? –
爲了回答您的問題並解決代碼中的所有錯誤,我們基本上必須向您介紹Java編程語言。堆棧溢出不是地方的任務。 –