我希望能够让用户开始新一轮游戏或结束游戏。每次当我被问到是否要继续(n / y)时我都不回答它并不会结束游戏。
另外我如何计算比赛次数?私有静态变量也在我没有做出的赋值中给出。
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);
}
}
}
答案 0 :(得分:0)
while(endStart == true) or while( endStart )
看起来会更好,而while(endStart = true),绝不比较endStart变量,再加上你是不可避免的回归每次你在PlayGame()方法中调用PlayGame()一次,并且它一直变得越来越疯狂
答案 1 :(得分:0)
你可以制作一个boolean
,你可以在循环中查看它,例如:
boolean test = true;
do{
//your code
//if you want to break you can change the test variable
...
if(val.equalsIgnoreCase("n")){
System.out.println(THANKS_MSG);
test = false;
break();
}
..
}while(guess > MIN_NUMBER || guess < MAX_NUMBER || test);
因此,如果您的情况或测试结果为假,您将从循环中断开。
答案 2 :(得分:0)
你可以使用带标签的break,Labeled break语句用于终止外部循环,循环应该被标记为可以工作。举例来说,
loop1:
for(){
...
for(){
....
break loop1;
}
}