问题
In this game that I created I ran into a problem. It all works great except that if you fail for some reason the game just restarts and that's not what I want. I want to display what I have it set to display and then break the loop, for some reason the break;
isn't working.
Code:
import java.util.Scanner;
import java.util.Random;
public class GuessingGame1_3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
System.out.print("Pick a number to guess between: ");
int userNumber = input.nextInt();
int random = rand.nextInt(userNumber);
if (random < 1) {
random = random + 1;
} else if (random > userNumber) {
random = random - 1;
}
while (true) {
System.out.print('\f');
System.out.print("Pick the amount of attempts you would like to have (Max of 10 attempts): ");
int userAttempts = input.nextInt();
if (userAttempts > 10) {
System.out.println("To Many Attempts");
System.out.println("");
} else if (userAttempts <= 10) {
System.out.println("Version: 1.3");
System.out.println("----------------------------------------------------------------------");
System.out.println("You have " + userAttempts + " attempts to guess the number or else you fail. Goodluck!");
System.out.println("");
System.out.println("Type 'begin' to Begin!");
String start = input.next();
System.out.print('\f');
if (start.equals("begin")) {
for(int i=1; i<userAttempts + 1; i++) {
System.out.print("Enter a number between 1-" + userNumber + ": ");
int number = input.nextInt();
if (number > random) {
System.out.println("Too Big");
System.out.println("");
} else if (number < random) {
System.out.println("Too Small");
System.out.println("");
} else if (number == random) {
System.out.println("Correct!");
break;
}
if (i == userAttempts) {
System.out.println("You have failed");
System.out.println("Number Was: " + random);
break;
}
}
} else if (!start.equals("begin")) {
System.out.print('\f');
System.out.println("Incorrect Command");
System.out.println("Please Exit Console And Retry");
break;
}
}
}
}
}
回答1:
With nested loops if you want to break
the outer loop you might add a label prefix1.
game: while (true) {
then you could
break game;
to terminate the statement that is labeled game
.
1See also JLS-14.7. Labeled Statements which says (in part) Statements may have label prefixes.
回答2:
Introduce a variable to check if you should continue the loop. Something like:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
System.out.print("Pick a number to guess between: ");
int userNumber = input.nextInt();
int random = rand.nextInt(userNumber);
if (random < 1) {
random = random + 1;
} else if (random > userNumber) {
random = random - 1;
}
boolean continueGame = true;
while (continueGame) {
System.out.print('\f');
System.out
.print("Pick the amount of attempts you would like to have (Max of 10 attempts): ");
int userAttempts = input.nextInt();
if (userAttempts > 10) {
System.out.println("To Many Attempts");
System.out.println("");
} else if (userAttempts <= 10) {
System.out.println("Version: 1.3");
System.out
.println("----------------------------------------------------------------------");
System.out
.println("You have "
+ userAttempts
+ " attempts to guess the number or else you fail. Goodluck!");
System.out.println("");
System.out.println("Type 'begin' to Begin!");
String start = input.next();
System.out.print('\f');
if (start.equals("begin")) {
for (int i = 1; i < userAttempts + 1; i++) {
System.out.print("Enter a number between 1-"
+ userNumber + ": ");
int number = input.nextInt();
if (number > random) {
System.out.println("Too Big");
System.out.println("");
} else if (number < random) {
System.out.println("Too Small");
System.out.println("");
} else if (number == random) {
System.out.println("Correct!");
continueGame = false;
}
if (i == userAttempts) {
System.out.println("You have failed");
System.out.println("Number Was: " + random);
continueGame = false;
}
}
} else { //You may remove if (!start.equals("begin")) check
//as the result is either true or false and you
//have checked the true condition in if
System.out.print('\f');
System.out.println("Incorrect Command");
System.out.println("Please Exit Console And Retry");
continueGame = false;
}
}
}
}
回答3:
You need to break the outer loop as well the inner loops
Take a look : (variable failattempt
)
import java.util.Scanner;
import java.util.Random;
public class GuessingGame1_3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
System.out.print("Pick a number to guess between: ");
int userNumber = input.nextInt();
int random = rand.nextInt(userNumber);
if (random < 1) {
random = random + 1;
} else if (random > userNumber) {
random = random - 1;
}
boolean failattempt = false;
while (true) {
System.out.print('\f');
System.out.print("Pick the amount of attempts you would like to have (Max of 10 attempts): ");
int userAttempts = input.nextInt();
if (userAttempts > 10) {
System.out.println("To Many Attempts");
System.out.println("");
} else if (userAttempts <= 10) {
System.out.println("Version: 1.3");
System.out.println("----------------------------------------------------------------------");
System.out.println("You have " + userAttempts + " attempts to guess the number or else you fail. Goodluck!");
System.out.println("");
System.out.println("Type 'begin' to Begin!");
String start = input.next();
System.out.print('\f');
if (start.equals("begin")) {
for(int i=1; i<userAttempts + 1; i++) {
System.out.print("Enter a number between 1-" + userNumber + ": ");
int number = input.nextInt();
if (number > random) {
System.out.println("Too Big");
System.out.println("");
} else if (number < random) {
System.out.println("Too Small");
System.out.println("");
} else if (number == random) {
System.out.println("Correct!");
break;
}
if (i == userAttempts) {
System.out.println("You have failed");
System.out.println("Number Was: " + random);
failattempt = true;
break;
}
}
if (failattempt == true)
break;
} else if (!start.equals("begin")) {
System.out.print('\f');
System.out.println("Incorrect Command");
System.out.println("Please Exit Console And Retry");
break;
}
}
}
}
}
来源:https://stackoverflow.com/questions/32793478/cant-stop-while-loop