Question

In: Computer Science

The goal of this assignment is to give you some experience writing code with control structures...

The goal of this assignment is to give you some experience writing code with control structures in Java.

You will create a number guessing game program. The program will randomly choose a number between 0 and 100 that the user will try to guess. The user will be given a maximum number of tries (10 max) to guess the number. The user wins only when they guess the randomly chosen number before the maximum number of tries expires. Otherwise, the user loses the game. During the course of playing the game, you need to let the user know whether the guess was too high or too low.

The program should display instructions on how to play the game, number of wins, losses, and the total number of guesses.

I want you to use the Java API or other online resources to find out how to generate a random number.

Solutions

Expert Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.InputMismatchException;

import java.util.Random;

import java.util.Scanner;

public class NumberGuess {

   public static void main(String[] args) {

       Random random = new Random();

       Scanner sc = new Scanner(System.in);

       int guess;

       int looses = 0, wins= 0;

       while(true){

           System.out.println("Guess a number in range 0-100");

           int num = random.nextInt(101); //random number in range 0-100

           int i = 1;

           while(i <= 10){

               try{

                   System.out.print("Enter your guess: ");

                   guess = sc.nextInt();

                   if(guess == num){

                       System.out.println("You have guessed correctly!!");

                       wins++;

                       break;

                   }else if(guess>num){

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

                   }else{

                       System.out.println("You guess is too low");

                   }

               }catch(InputMismatchException e){

                   System.out.println("Invalid input. Please enter integer value");

                   sc.next(); // this consumes the invalid token

               }

               i++;

           }

          

           if( i == 11)

               looses++;

          

           // User Input

           System.out.println("Do you want to play again(y/n) ? ");

           char op = sc.next().charAt(0);

           if(op != 'y' && op != 'Y')

               break;

       }

      

       System.out.println("Number of games played: "+(wins+looses));

       System.out.println("Number of wins: "+wins);

       System.out.println("Number of losses: "+looses);

   }

}

/*

Sample run:

Guess a number in range 0-100

Enter your guess: 45

You guess is too low

Enter your guess: 60

You guess is too low

Enter your guess: 70

Your guess is too high

Enter your guess: 65

You guess is too low

Enter your guess: 67

You guess is too low

Enter your guess: 68

You guess is too low

Enter your guess: 69

You have guessed correctly!!

Do you want to play again(y/n) ?

n

Number of games played: 1

Number of wins: 1

Number of losses: 0

*/


Related Solutions

java Goal: This lab will give you practice writing code that uses inheritance and Java interfaces....
java Goal: This lab will give you practice writing code that uses inheritance and Java interfaces. Please make sure you have a partner for this lab. No code is provided with this lab. Write code to experimentally resolve the following questions about Java. You will need to show your code to the TA or lab assistant. Part I: Assignments and Casting (1 point) ------------------------------------------ Let Y be a subclass of X, and let y and x be variables of classes...
1. Introduction This assignment will give you some experience working with C input (using scanf()) and...
1. Introduction This assignment will give you some experience working with C input (using scanf()) and output (using printf()), as well as arithmetic operations with variables. You will do some very basic circuit analysis, reading the values of a voltage source and three resistors, and calculating the voltage across and current through each of the resistors using three different circuit configurations. Test cases for the program can be found at this link. Remember, in addition to submitting your code, you...
psychology eleventh Chapter 6   Sensory Experience Writing (Sensory Experience Writing Assignment Directions and Rubric) write a...
psychology eleventh Chapter 6   Sensory Experience Writing (Sensory Experience Writing Assignment Directions and Rubric) write a one page analysis of the interactions of sensation on perception.  To be more clear, how do the senses work together to give us the experiences we perceive in the world around us?  The finished product should be one page in length with 12 point font and margins of 1 inch.
The purpose of this exercise is to give you experience in writing condition-controlled loops. Create a...
The purpose of this exercise is to give you experience in writing condition-controlled loops. Create a code in Python that will print monthly loan amortization schedule given the loan amount (P), APR i.e. Annual Percentage Rate (r=APR in %/12), loan terms in number of months (n). Please use these formulas: Monthly Payment (A) = P * ((r * (1 + r)**n)/(((1+r)**n)-1))            Monthly Interest Pmt (mIP)= r * Total Remaining Balance Monthly Principal Pmt (mPP) = A –...
Overview The purpose of this assignment is to give you experience designing a database and building...
Overview The purpose of this assignment is to give you experience designing a database and building an Entity Relationship Diagram in a business scenario. Submit all tasks in one document. Include headings in your document. The organization is a dentist's office. When new patients are seen for the first time, they complete a patient information form that asks for their name, address, phone number, and brief medical history, which are stored in the patient information file. When a patient calls...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you will be using a special version of a linked-list, called a Stack. Stacks are linear structures that are used in many applications. In fact, Java's runtime environment (JRE) uses a stack to evaluate Java's byte-code. You can learn more about Stacks from this URL: Tutorialspoint - Stack (Links to an external site.) The stack description above uses an array implementation for examples. You will...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you will be using a special version of a linked-list, called a Stack. Stacks are linear structures that are used in many applications. In fact, Java's runtime environment (JRE) uses a stack to evaluate Java's byte-code. You can learn more about Stacks from this URL: Tutorialspoint - Stack (Links to an external site.) The stack description above uses an array implementation for examples. You will...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you will be using a special version of a linked-list, called a Stack. Stacks are linear structures that are used in many applications. In fact, Java's runtime environment (JRE) uses a stack to evaluate Java's byte-code. You can learn more about Stacks from this URL: Tutorialspoint - Stack (Links to an external site.) The stack description above uses an array implementation for examples. You will...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you will be using a special version of a linked-list, called a Stack. Stacks are linear structures that are used in many applications. In fact, Java's runtime environment (JRE) uses a stack to evaluate Java's byte-code. You can learn more about Stacks from this URL: Tutorialspoint - Stack (Links to an external site.) The stack description above uses an array implementation for examples. You will...
Decision Structures Calorie Calculator Assignment Overview This assignment will give you practice with numerical calculations, simple...
Decision Structures Calorie Calculator Assignment Overview This assignment will give you practice with numerical calculations, simple input/output, and if-else statements. Program Objective: In this exercise, you will write a program the calculates the Daily Caloric needs for weight lose diet. According to everydayhealth.com they recommend this daily caloric formula for a weight loss diet: BMR + Activity Level - 500 calories. Calories Calculator The Harris-Benedict equation estimates the number of calories your body needs to maintain your weight if you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT