我希望能夠讓用戶開始新一輪或結束遊戲。每次詢問我是否想繼續(n/y)時,我都會回答否,這並不會結束遊戲。我如何擺脫這個循環? java
我又如何計算出回合數?另外私人靜態變量在我沒有做的任務中給出。
import java.util.Scanner;
import java.util.Random;
public class GuessingGame {
private static final int MIN_NUMBER = 1;
private static final int MAX_NUMBER = 205;
private static final int QUIT_VALUE = -1;
private static final int LOSE_VALUE = -2;
private static final int MAX_GAMES = 4;
private static final int MAX_GUESSES = 10;
private static final int HINT_THRESHOLD = 5;
private static final int BACKDOOR_VALUE = -314;
private static final String NOPE_MSG = "nope...";
private static final String NOPE_NOPE_MSG
= "you've already guessed that wrong guess...";
private static final String INVALID_INPUT_BEGIN
= "*** invalid input -- ";
private static final String INVALID_INPUT_LESS_MIN_MSG
= INVALID_INPUT_BEGIN + "must be greater than " + (MIN_NUMBER - 1);
private static final String INVALID_INPUT_GREATER_MAX_MSG
= INVALID_INPUT_BEGIN + "must be less than " + (MAX_NUMBER + 1);
private static final String INVALID_INPUT_YN_MSG
= INVALID_INPUT_BEGIN + "must be n or y";
private static final String WINNER_MSG
= "you're a winner... # of guesses: ";
private static final String LOSER_MSG
= "too many guesses entered... the number was ";
private static final String QUITTER_MSG
= "you're a quitter... the number was ";
private static final String MAX_GAMES_PLAYED_MSG
= "you've played the maximum number (" + MAX_GAMES + ") of games";
private static final String ENTER_GUESS_PROMPT
= "enter a guess between " + MIN_NUMBER + " and " + MAX_NUMBER
+ " (" + QUIT_VALUE + " to quit): ";
private static final String PLAY_AGAIN_PROMPT
= "Do you want to play again (n or y)? ";
private static final String BOLD_BEGIN = "*** ";
private static final String BOLD_END = " ***";
private static final String PLAY_MSG = " playing the CSC" + MAX_NUMBER
+ " guessing game." + BOLD_END;
private static final String WELCOME_MSG
= BOLD_BEGIN + "Hello! Have fun" + PLAY_MSG;
private static final String THANKS_MSG
= BOLD_BEGIN + "Thanks for" + PLAY_MSG;
public static void main(String[] args) {
PlayGame();
}
static Random rng = new Random(System.currentTimeMillis());
static Scanner stdin = new Scanner(System.in);
static Scanner scan = new Scanner(System.in);
static int n = MIN_NUMBER + rng.nextInt(MAX_NUMBER);
static int guess;
static int guessCounter = 0;
static boolean endStart = true;
public static void PlayGame() {
System.out.println(WELCOME_MSG);
while (endStart = true) {
do {
System.out.println();
System.out.print(ENTER_GUESS_PROMPT);
guess = stdin.nextInt();
guessCounter = guessCounter + 1;
if (guess < MIN_NUMBER && guess != BACKDOOR_VALUE
&& guess != QUIT_VALUE) {
System.out.println(INVALID_INPUT_LESS_MIN_MSG + "\n");
}
if (guess > MAX_NUMBER) {
System.out.println(INVALID_INPUT_GREATER_MAX_MSG + "\n");
}
if (guess == BACKDOOR_VALUE) {
System.out.println(n);
}
if (guess == QUIT_VALUE) {
System.out.println(QUITTER_MSG + n);
System.out.println(PLAY_AGAIN_PROMPT);
String val = scan.next();
if (val.equalsIgnoreCase("y")) {
PlayGame();
}
if (val.equalsIgnoreCase("n")) {
System.out.println(THANKS_MSG);
break;
}
}
if (guessCounter == MAX_GUESSES) {
System.out.println(LOSER_MSG + n
+ PLAY_AGAIN_PROMPT);
String val = scan.next();
if (val.equalsIgnoreCase("y")) {
PlayGame();
}
if (val.equalsIgnoreCase("n")) {
System.out.println(THANKS_MSG);
break;
}
}
if (guess > MIN_NUMBER || guess < MAX_NUMBER) {
if (guess != n || guess == BACKDOOR_VALUE) {
System.out.println(NOPE_MSG);
}
if (guess == n) {
System.out.println(WINNER_MSG + guessCounter);
System.out.println(PLAY_AGAIN_PROMPT);
String val = scan.next();
if (val.equalsIgnoreCase("y")) {
PlayGame();
}
if (val.equalsIgnoreCase("n")) {
System.out.println(THANKS_MSG);
break;
}
}
}
} while (guess > MIN_NUMBER || guess < MAX_NUMBER);
}
}
}
太多的代碼行和太多的問題。你打破了循環與休息。 – efekctive
@efekctive我知道我可以使用休息,但我想知道如何擺脫特定的循環。 –
然後減少示例代碼。如果你知道如何突破一次,第二次是一樣的 – efekctive