我正在进行一个猜数字游戏,我在设置循环方面遇到了问题,让玩家可以再次玩游戏。我还想知道是否有办法在循环中包含提示选项。
我已经编程了2个月。
package carlosnumber;
import java.util.Random;
import java.util.Scanner;
public class CarlosNumber {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
String name = "start";
int number = 0;
int numTries = 0;
int secretNumber;
String wantHints, playagain;
int guess = -1, numGuess, wonGames, quitGames, playedGames;
boolean end = false;
boolean hint = false;
boolean play = true;
String win = null;
System.out.println("Welcome the the Guessing game");
while (play == true) {
do {
Random secretNumberGenerator = new Random();
secretNumber = 56;
if (name.equals("start")) {
System.out.println("To Start playing, please enter your name");
name = console.nextLine();
System.out.println("The system will randomly select a number between 1 and 100"
+ "\nYour goal is to guess the number. At each turn enter"
+ " in your guess, and I will tell you when your guess is correct");
}
System.out.println("Please Enter a number");
number = console.nextInt();
numTries++;
if (number < secretNumber) {
System.out.println("Your guess is low");
}
if (number > secretNumber) {
System.out.println("Your guess is high");
}
if (number == secretNumber) {
System.out.println("YOU WIN!!" + "It took you " + numTries + " Tries");
end = true;
}
} while (end == false);
System.out.println("Do you wish to play again? Y/N");
playagain = console.nextLine();
if (playagain == "Yes") {
play = true;
} else {
play = false;
}
}
}
}