I want to be able to get the user to either start a new round or end the game. Every time I answer no when asked if I want to continue(n/y) it does not end the game.
Also how do I count the number of rounds played? Also the private static variables are given in the assignment I did not make them.
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);
}
}
}
while(endStart == true) or while( endStart )
看起来会更好,而while(endStart = true),绝不是endStart变量的比较,再加上你是不可避免的回归每当你在PlayGame中调用PlayGame()一次获得时( )方法,它一直变得越来越疯狂
You can make a boolean
and you can check with it in your loop for example :
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);
So if your condition or test is false you will break from your loop.
You Can use a break with label, Labeled break statement is used to terminate the outer loop, the loop should be labeled for it to work. For an example follow this,
loop1:
for(){
...
for(){
....
break loop1;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.