简体   繁体   中英

printing a print statement after a certain amount of tries in a loop

I am new to coding. I created a guessing game, and it works well but, I would like to know how to make it so that after the user attempts guessing the number 3 times, they get a hint which I put on the last line, but it is currently unreachable, and I do not know how to make the statement reachable, and in the do while loop. I am currently stuck. Thank you

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;
   }  
}

There are a few flow issues with your program, but here is a simple way to fix it up.

First of all, you are not actually using the value of guess when you return it from your run() method, so that can be removed.

Also, in cases like this, you would not want to use a do/while loop, but just while . You want to keep repeating the prompt until the user guesses correctly. So add a boolean to allow you to check if they won:

boolean correct = false; - We set it false to begin because they haven't won yet.

Now, instead of calling run() again after every guess (which resets the tries count every time, just let the while loop do its job and repeat itself. Hence, we need to move the prompt input into the while loop.

Here is a complete code listing of the changes:

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();
    }    

    // Change the return type to void as you never use the value returned
    public static void run(Scanner kb) {    
        int num = 44;

        // Add a boolean to determine if the game is won
        boolean correct = false;

        int tries = 0;

        while (!correct) {

            System.out.println("Please enter a number between 1-100");
            int guess = kb.nextInt();

            if (guess < num) {
                System.out.println("Oooh. Your guess is too low. Try again.");
                System.out.println();
            } else if ((guess > 100) || (guess < 0)) {
                System.out.println("That isn't between 1-100 is it?");
                System.out.println();
            } else if (guess > num) {
                System.out.println("Aaah. Your guess is too high. Try again.");
                System.out.println();
            } else if (guess == num) {

                // Flag the guess as correct; this will exit the loop after this run
                correct = true;

                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++;
        }    
    }
}

1.You are recursively calling run() method and every time you call this method, a new variable try will be created and initialised to zero. 2. Your recursive call is before the condition check and due to the same reason the logic may never reach the condition check.

To make this work with minimal changes, you can use the following code. But this is not the best as it does not resolve the above shortcomings

import java.util.Scanner;

public class guessing_game {

static int tries = 0;

   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;


       do{

           tries++;
           if(tries>=3)  break;

       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);
               }
         }

         }while( tries < 3);
            {
               System.out.print("Here's a hint the lucky number is 4");
            }

           return guess;
   }




}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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