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...
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...
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...
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...
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...
Hello i am working on an assignment for my programming course in JAVA. The following is...
Hello i am working on an assignment for my programming course in JAVA. The following is the assignment: In main, first ask the user for their name, and read the name into a String variable. Then, using their name, ask for a temperature in farenheit, and read that value in. Calculate and print the equivalent celsius, with output something like Bob, your 32 degrees farenheit would be 0 degrees celsius Look up the celsius to farenheit conversion if you do...
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT