我正在尝试为我目前正在使用循环的作业构建一个猜谜游戏。我试图让我的游戏让用户可以选择再次玩。我在尝试设置最后一段代码时遇到了困难,所以我按照 youtube 上的一个示例进行操作。虽然我已经声明了变量 restart 编译器一直告诉我我没有初始化它。对此代码的任何帮助将不胜感激
import java.util.Scanner;
import java.util.Random;
public class High_Low_Game {
public static void main (String[] args){
Random rand=new Random();
Scanner scan= new Scanner(System.in);
int guess,num,count=0;
String restart;
num=rand.nextInt(99+1);
do{
System.out.println("Please enter a number from 1-100,press 0 to quit");
guess=scan.nextInt();
count++;
while(guess!=0)
if(guess>num){
System.out.println("Your guess was too high, try again");
guess=scan.nextInt();
count++;}
else
if(guess<num){
System.out.println("The number is too low,enter another guess ");
guess=scan.nextInt();
count++;}
else
if(guess==num){
System.out.println("You have guessed correctly");
System.out.println("It took you "+count+ " guesses");
System.out.println("Would you like to play again? (Y/N)");
restart=scan.next();}
}while(restart.equals("Y"));
}
}