Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I\'m having trouble with my play again sequence in my program and I\'m hopin

ID: 3621705 • Letter: H

Question

Hi, I'm having trouble with my play again sequence in my program and I'm hoping someone can help me resolve this. Thanks!

Here is my program:

// This program generates a random number and askes the user to guess what the number is

import java.util.Scanner;
import java.util.Random;

public class GuessingGame
{
   public static void main(String [] args)
   {
      //Create a scanner object to read from the keyboard
      Scanner kb = new Scanner(System.in);

      //Create a random object
      Random rand = new Random();

      //Identifier declarations
      int num = rand.nextInt(100) + 1;
      int guess = 0;
      int count = 0;
      int guesses = 0;
     
      //Title   
      System.out.println("The Guessing Game --");
     
      do
      {
         System.out.println("Guess what number I have (1-100)? ");
         guess = kb.nextInt();
         guesses ++;

         if(num < guess) {
            System.out.println("Too high, try again.");
         } else if(num > guess) {
            System.out.println("Too low, try again.");
         } else {
            System.out.println("You're right, the number is " + num);
            System.out.println("You guessed " + guesses + " times");
         }
      }
      while(guess!=num);
     
      boolean play = false;
      String input;
      do
      {
         System.out.print("Play again ('y' or 'n')? ");
         input = kb.nextLine().toLowerCase();

         if(input.equals("y")) {
            play = true;
         }else if(input.equals("n")) {
            play = false;
         }else {
            System.out.println("Invalid, enter again: ");
            input = kb.nextLine().toLowerCase();
         }
       }
       while(play==true);             
   }
}

Explanation / Answer

Dear, Here is the code Modified the code it works for multiple times game untill user inputs 'n' import java.util.Scanner; import java.util.Random; public class GuessingGame { public static void main(String [] args) { //Create a scanner object to read from the keyboard Scanner kb = new Scanner(System.in); //Create a random object Random rand = new Random(); //Identifier declarations int num; int guess = 0; int count = 0; int guesses = 0; //Title System.out.println("The Guessing Game --"); boolean play = true; while(play) { num= rand.nextInt(100) + 1; do { System.out.println("Guess what number I have (1-100)? "); guess = kb.nextInt(); guesses ++; if(num guess) { System.out.println("Too low, try again."); } else { System.out.println("You're right, the number is " + num); System.out.println("You guessed " + guesses + " times"); } } while(guess!=num); String input; System.out.print("Play again ('y' or 'n')? "); input = kb.nextLine().toLowerCase(); if(input.equals("y")) { play = true; } else if(input.equals("n")) { play = false; } else { System.out.println("Invalid, enter again: "); input = kb.nextLine().toLowerCase(); } } } } Hope this will help you.
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote