Question

In: Computer Science

This is for java For my assignment, I was supposed to make a number guessing game....

This is for java

For my assignment, I was supposed to make a number guessing game. The class, HiLo, should pick a random number between 1 and 100 (inclusive). Then, prompt the user to guess the number. On each guess, print if the user is correct or if the guess is too high or too low. Allow only 5 guesses. In the end, print out the correct answer and how many attempts were made.

Then give the user the option to play again (1 for yes and 0 for no).

I have tried to do this but I get stuck on making it so that there are only 5 guesses and I always get an error when asking the option to play again.

Solutions

Expert Solution

Below is your code

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

public class HiLo {
   public static void main(String[] args) {
       boolean done = false;
       Scanner sc = new Scanner(System.in);
       while(!done) {
           int count = 0;
           Random rand = new Random();
           int number = rand.nextInt(101) + 1;
           int guess;
           while(count < 5) {
               System.out.print("Guess a number: ");
               guess = sc.nextInt();
               count++;
               if(guess < number) {
                   System.out.println("Guess is too low.");
               } else if (guess > number) {
                   System.out.println("Guess is too high.");
               } else {
                   System.out.println("Thats correct. You guessed it right in "+count+" guesses.");
                   break;
               }
           }
           if(count >= 5) {
               System.out.println("Maximum tries over. You have lost the game. Please try again.");
           }
           String choice;
           System.out.print("Do you want to play again ? (Y/N): ");
           choice = sc.next();
           if(choice.equalsIgnoreCase("y")) {
               done = false;
           } else {
               done = true;
               System.out.println("Thanks for playing the game.");
           }
       }
       sc.close();
   }
}


Related Solutions

guessing game in Java. It will have a guess input used for guessing the random number...
guessing game in Java. It will have a guess input used for guessing the random number that is generated from 1 - 100. When the user makes a guess it will tell them if the number is lower or higher than the guess. There is also a choice to give up which then reveals the correct number. The last choice will be new game which resets the random number. Last, the program should keep counts of tries. When the user...
Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
Assignment - Number Guessing with a Class For this assignment you will revisit the number guessing...
Assignment - Number Guessing with a Class For this assignment you will revisit the number guessing game in which the user picks a number, and your program tries to guess the number. For review, a sample run of the program is printed below. In the example the user picks the value 72 for the first game, and then 25 for the second game. The user's input is in bold. Here is how the output might look (exact numbers may vary)...
JAVA Write a number guessing game that will generate a random number between 1and 20 and...
JAVA Write a number guessing game that will generate a random number between 1and 20 and ask the user to guess that number. The application should start by telling the user what the app does. You should then create the random number and prompt the user to guess it. You need to limit the number of times that the user can guess to 10 times. If the user guesses the number, display some message that tell them that they did...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
In JAVA, write a number guessing game that randomly assigns a integer between one and twenty...
In JAVA, write a number guessing game that randomly assigns a integer between one and twenty for the user to guess. Write three methods to make this particular program work. 1. Have a method which generates a random number and returns the value to the main (stored as a variable) 2. Have a method which prompts the user for a guess. Return the guess to the main. 3. Evaluate the guess and the random "secret" number by parameters, and then...
in java Create simple number guessing game as follows. First, prompt the user to enter a...
in java Create simple number guessing game as follows. First, prompt the user to enter a min and max value. In your code, declare and assign a variable with a random integer in this range (inclusive). Then, ask the user to guess the number. Input the user's guess. And at the end output "Correct!" if the user's guess was right on spot, or "Sorry, the answer is not corrcet.” The number I was looking for was xxx. Replace xxx with...
Guessing game - when I guess, the guess that I make doesn't check correct even though...
Guessing game - when I guess, the guess that I make doesn't check correct even though it is correct. // use external file: line1 linetwo line three linefour line five // end external file. // begin program: #include <iostream> #include <fstream> #include <iomanip> #include <string> #include <time.h> #include <stdlib.h> using namespace std; string trimWSFuntion(string); void guessMess(string, string); int main() { //to store 3 lines string lineFromWordtxt1; string lineFromWordtxt2; string lineToTrim; ifstream myFile; string read_file_name; // this block gets the filename...
You are asking to develop a “Triangle Guessing” game in Python for the assignment. The program...
You are asking to develop a “Triangle Guessing” game in Python for the assignment. The program accepts the lengths of three (3) sides of a triangle as input from a player. The program output should indicate whether the triangle is a right triangle, an acute triangle, or an obtuse triangle. 1.Tips to help you for this assignment: Make sure each side of the triangle is a positive integer. Use try/except for none positive integer input. 2. Validating the triangle. That...
this is my assignment for intro to java tha i could not figure out, my system...
this is my assignment for intro to java tha i could not figure out, my system just crashed wit a lot of errors. thank for your help Create a new Java class inside your project folder. The name of the class should be: TempConverter Note that this means you should have the following line at the top of your program: public class TempConverter Write a program that allows the user to convert a temperature given in degrees Celsius or Fahrenheit...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT