0
猜測嘗試提示用戶再次玩遊戲的遊戲。當我提示用戶再次玩遊戲後,遊戲回到它之前的隨機數而不是重新啓動
我將它設置爲一個while循環,但由於某種原因,它仍然使用前一個遊戲的相同數量。這是爲什麼?我是否必須在while循環中添加更多細節?
public class Guessing_zulueta {
public static int getOneInt() {
//we will get one integer from the keyboard
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer: ");
return in.nextInt();
}
public static int random = getRand();
public static final int MAX = 100;
public static int getRand() {
Random randGenerator = new Random();
int x = randGenerator.nextInt(MAX);
return x;
}
public static void main(String[] args) {//here is my problem
while (true){
guessingGame();
System.out.println("Do you wish to play again? (1 for yes, -1 for no: ");
Scanner scan2 = new Scanner(System.in);
int val = scan2.nextInt();
if (val == 1)
guessingGame();
if (val == -1)
break;
}
}
public static void guessingGame() {
int input = getOneInt();
if (input == random) {
System.out.println("Congratulations");
}
if (input > random) {
System.out.println("Too big.");
guessingGame();
}
if (input < random) {
System.out.println("Too small.");
guessingGame();
}
}
}
這樣做是爲了在每次遊戲後而不是每場遊戲後隨機化數字 –
對不起,我最初誤讀了您的代碼。我已經更新了我的答案。 – DanielGibbs