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

To learn to use do-while loops. You should generate a random number between 1 an

ID: 3815919 • Letter: T

Question

To learn to use do-while loops. You should generate a random number between 1 and 10. then prompt the user for a guess. If the guess is incorrect then you should print "Nope" if the guess is correct you should print "Well done!". Once the user has guess correctly you should ask them to type 0 to stop the game or any other number to play again. Get the program to generate and output the random number first Then get the program to play the game just once Finally get the game to repeat. You will need to use "nested loops" to complete this assignment.

Explanation / Answer

package org.students;

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

public class GuessGame1 {

   public static void main(String[] args) {
       //Declaring variables
       int random_number,guess,choice;
      
       //Crating the random class object
       Random r = new Random();
      
       //Scanner object is used to get the inputs entered by the user
               Scanner sc=new Scanner(System.in);
              
               //This loop continues to execute until the user enters 0
               while(true)
               {
                   //generating the random number
                   random_number=r.nextInt(10) + 1;
                  
                   while(true)
                   {
                       //getting the user guessed number entered by the user
                       System.out.print("What's your guess ?");
                       guess=sc.nextInt();
                      
                       /* if the generated random number is equal to
                       * user guessed number then display message
                       */
                       if(random_number==guess)
                       {
                           System.out.println("Well done!");
                           break;
                       }
                       else
                       {
                           System.out.println("Nope.");
                           continue;
                       }
                          
                   }
                  
                   //Prompting the user to play again or not
                   System.out.print("Play again ? (0 to stop ):");
                   choice=sc.nextInt();
                   if(choice==0)
                   break;
                   else
                   continue;  
                  
               }
           System.out.println("Thanks for Playing. ");

   }

}

___________________

output:

What's your guess ?6
Nope.
What's your guess ?5
Nope.
What's your guess ?8
Nope.
What's your guess ?9
Nope.
What's your guess ?4
Well done!
Play again ? (0 to stop ):1
What's your guess ?5
Nope.
What's your guess ?6
Nope.
What's your guess ?7
Nope.
What's your guess ?9
Nope.
What's your guess ?3
Well done!
Play again ? (0 to stop ):0
Thanks for Playing.

_________Thank 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