繁体   English   中英

如何在数字猜谜游戏中循环时重新启动

[英]How to restart while loop in Number Guessing Game

我对此代码有疑问。 我所看到的问题是,当用户运行代码并正确猜测或使用最大尝试次数时,while循环不会启动,并允许游戏重新启动。 我的想法是将计数器(尝试次数)重置为零,然后重新开始,但是我只是获得带有总得分的退出消息。

int guess = 0;
     int attempt = 0;
     int number = new Random().nextInt(1000) + 1;
     int scorePlayer = 0; int scoreComputer = 0;
     boolean repeat;

     System.out.println("Hello and welcome to the number guessing game.");
     System.out.println("Please guess a number between 1 and 1000");


    while(repeat == true){
         do{
             Scanner scan = new Scanner(System.in);
             guess = Integer.parseInt(scan.nextLine());
             System.out.println("The number you guessed is "+guess);
             attempt++;

             if(guess == number){
                 System.out.println("Congratulations! You got it!!");
                 scorePlayer++;
                 System.out.println("Would you like to play again? [YES/NO]");
                 Scanner a = new Scanner(System.in);
                 String answer = a.nextLine().toUpperCase();
                 if (answer != "YES"){
                     //repeat = false;
                     System.out.println("Thank you for playing");
                     System.out.println("The final score is "+scorePlayer+" to you and "+scoreComputer+" to the server");
                     if (scorePlayer<scoreComputer){
                         System.out.println("Computer wins!");
                     }else{
                         System.out.println("You win!");
                     }break;
                 }else{
                     attempt = 0;
                 }
             }else if(guess < number){
                 System.out.println("That is incorrect, try a larger number");

             }else if(guess > number){
                 System.out.println("That is incorrect, try a smaller number");

             }if(attempt == 10 && guess != number){
                 System.out.println("That is the max number of guesses allowed");
                 System.out.println("The number you were looking for is "+number);
                 scoreComputer++;
                 System.out.println("Would you like to play again? [YES/NO]");
                 Scanner b = new Scanner(System.in);
                 String answer = b.nextLine().toUpperCase();
                 if (answer != "YES"){
                     //repeat = false;
                     System.out.println("Thank you for playing");
                     System.out.println("The final score is "+scorePlayer+" to you and "+scoreComputer+" to the server");
                     if (scorePlayer<scoreComputer){
                         System.out.println("Computer wins!");
                     }else{
                         System.out.println("You win!");
                     }break;
                 }else{
                     attempt = 0;
                 }
             }

         }while(guess != number || attempt == 0);
         repeat = false;
    }

单“ =”是赋值运算符,只需将其更改为双“ ==”。

while(repeat == true)

除了ugur所说的,您还可以做

while(repeat)

因为repeat是一个布尔变量(返回true或false)

检查您设置了repeat=false的外循环中的最后一行。 因此,当您从内部循环break时, while检查重复的while外部循环,每次除第一次外都为假。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM