经过一定次数的尝试后打印打印语句

我是编码新手。我创建了一个猜谜游戏,并且运行良好,但是我想知道如何制作它,以便用户尝试猜测该数字3次后,他们会得到一个提示,我将其放在最后一行,但是目前无法实现,而且我不知道如何在do while循环中使该语句可访问。我目前陷入困境。谢谢


import java.util.Scanner;


public class guessing_game {

   public static void main (String[] args) {

      Scanner kb = new Scanner(System.in);

      desc();

      run(kb);    


      //int nun = 0;


      //for (int i = 0; i < nun; nun ++)

   }

   public static void desc() {

      System.out.println("This is a guessing game.");

      System.out.println();

      System.out.println("Let's see how many tries it takes you to guess the right number!");

      System.out.println();

      System.out.println();

      System.out.println();

   }


   public static int run(Scanner kb) {

      System.out.println("Please enter a number between 1-100");

      int guess = kb.nextInt();


      int num = 44;


      int tries = 0;

      do {

         if (guess < num) {

            System.out.println("Oooh. Your guess is too low. Try again.");

            System.out.println();

            run(kb);

         }

         else if ((guess > 100) || (guess < 0)) {

            System.out.println("That isn't between 1-100 is it?");

            System.out.println();

            run(kb);

         }

         else if (guess > num) {

            System.out.println("Aaah. Your guess is too high. Try again.");

            System.out.println();

            run(kb);

         }

         else if(guess == num) {

            System.out.println("Bingo!!! Nice guess bud.");

            System.out.println("Tell a friend to play! Wanna try again? (y or n)");

            String choice = kb.next();

            if (choice.equalsIgnoreCase("y")) {

               run(kb);

            }

            else if (choice.equalsIgnoreCase("n")) {

               System.exit(0);

            }

         }

         tries++;

      }while(tries < 3);

      {

         System.out.print("Here's a hint the lucky number is 4");

      }


      return guess;

   }  

}


aluckdog
浏览 155回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java