我正在做一個任務,要求創建一個猜謎遊戲,但我已經編寫了猜謎遊戲。我需要問用戶他們是否想再次玩猜謎遊戲,我需要重新觸發循環。我不知道如何在第一個循環之後重新觸發,因爲它已經被執行和完成。如何在循環完成後重複循環
這裏是我的猜謎遊戲代碼:
import java.util.Random;
import java.util.Scanner;
public class ThreeEleven
{
public static void main (String[] args)
{
final int MAX = 100;
int answer, guess;
String again;
Scanner scan = new Scanner(System.in);
System.out.print ("I'm thinking of a number between 1 and "
+ MAX + ". Guess what it is: (or enter 0 to quit) ");
guess = scan.nextInt();
Random generator = new Random(); //Random generator. 1 to 100.
answer = generator.nextInt(MAX) +1;
//if (guess == answer){ //If guess equals answer
//System.out.println ("You got it! Good guessing!");
//}
//else if (guess == 0){ //Game ends
//System.out.println ("You have ended your game. Goodbye.");
//}
while (guess != answer && guess != 0){ //If guess and 0 is not answer, continue.
if (guess > answer && guess != 0){ //If guess is higher than answer
System.out.println ("You guessed too high!");
guess = scan.nextInt();
}
else{
if (guess < answer && guess != 0){ //If guess is lower than answer
System.out.println ("You guessed too low!");
guess = scan.nextInt();
}
else if (guess == answer){ //If guess equals answer
System.out.println ("You got it! Good guessing!");
}
else if (guess == 0){ //Game ends
System.out.println ("You have ended your game. Goodbye.");
}
}
}
if (guess == answer){
System.out.println ("You got it! Good guessing!");
//System.out.println ("You guessed " + + " times");
// System.out.println ("Want to play again? (yes or no)?");
// again = scan.next();
// if (again == "yes")
// {
// System.out.print ("I'm thinking of a number between 1 and "
// + MAX + ". Guess what it is: (or enter 0 to quit) ");
//guess = scan.nextInt();
// while (again == "yes")
// {
// while (guess != answer && guess != 0){ //If guess and 0 is not answer, continue.
// if (guess > answer && guess != 0){ //If guess is higher than answer
// System.out.println ("You guessed too high!");
// guess = scan.nextInt();
// }
// else{
// if (guess < answer && guess != 0){ //If guess is lower than answer
// System.out.println ("You guessed too low!");
// guess = scan.nextInt();
// }
// else if (guess == answer){ //If guess equals answer
// System.out.println ("You got it! Good guessing!");
// }
// else if (guess == 0){ //Game ends
// System.out.println ("You have ended your game. Goodbye.");
// }
// }
// }
// if (guess == answer){
// System.out.println ("You got it! Good guessing!");
// }
// System.out.println ("Want to play again? (yes or no)?");
//again = scan.next();
//}
//}
}
}
輸出:
I'm thinking of a number between 1 and 100. Guess what it is: (or enter 0 to quit) 50
You guessed too high!
40
You guessed too high!
30
You guessed too high!
20
You guessed too high!
10
You guessed too high!
1
You guessed too low!
5
You got it! Good guessing!
當你從我的代碼註釋中看到,我試圖複製循環而如果還添加else語句和一個問題來詢問用戶他們是否想要再次玩。有沒有更容易和更有效的方法來重新觸發循環,讓遊戲重新開始?
預先感謝您
我用你的代碼,它像是但是,當循環開始時,自動錶示我贏了,而不是讓我插入更多的數字。 –
@Learning_Java這意味着你必須將你的重要變量重置爲初始值_side_'while(playAgain)'循環,即在'while(playAgain)'之後和遊戲代碼之前。 – ajb
好吧,這是有道理的,謝謝大家的幫助! –