我試圖編寫一個程序,選擇團隊隨機管理,但是當我運行它時,每次都得到相同的4個團隊而不是不同的團隊?隨機輸出,無法正常工作
我試圖讓它每次產生一個隨機數時都會進入一個數組。然後我會檢查該數組,看看數字是否曾經用過。
任何幫助或建議,將不勝感激。謝謝!
import java.util.*;
class Start {
static String[] places = {"Man Utd", "Arsenal", "Aston Villa", "Chelsea",
"Everton", "Fulham", "Liverpool", "Man City", "Newcastle", "Norwich",
"QPR", "Reading", "Southampton", "Stoke", "Sunderland", "Swansea",
"Spurs", "West Brom", "West ham", "Wigan"};
static int[] NA = {21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21};
static Random rand = new Random();
static int RandInt = 0;
static boolean x = false;
static boolean p = false;
static int player = 1;
public static void main(String[] args) {
while (x != true) {
RandInt = rand.nextInt(places.length);
for (int k = 0; k <= NA.length; k++) {
while (p != true) {
if (RandInt == NA[k]) {
RandInt = rand.nextInt(places.length);
} else {
p = true;
NA[k] = RandInt;
}
}
System.out.println("player " + player + " is managing " + places[RandInt]);
player++;
p = false;
if (player >= 5) {
x = true;
System.exit(0);
}
}
}
}
}
int [] NA是什麼意思? – geoand
如果您使用答案,請選擇它。 – NonSecwitter