Question

This is a basic Java Question referencing Chapter 5 methods. The goal is to make a...

This is a basic Java Question referencing Chapter 5 methods. The goal is to make a quick program that has the computer guess a number, after which the user will input if they would like to play the easy, medium or hard level. After this point the user will be allowed to guess a certain amount of times, before the computer tells them they are correct, incorrect, and gives them the guessed number.

The Question is as follows:

Define a 'Guess the Number Game' with the following characteristics

a. The game picks a random number between 1-50 (key)

use random to get this number

b. The player needs to guess a number between 1-50

c. Ask player which level he or she wants to play with

-hard gets 3 chances (method)

-medium gets 6 chances(method)

-easy gets 10 chances(method)

D. If the player guesses a number less or greater than the key, a prompt is shown - "key is greater/lesser than the guess number"

E. If the player can pick the key in given number of attempts, prompt -- "You won!", else prompt "you loose."

This is what I have put together so far, I feel as though I may have it disorganized or in the wrong order.

import java.util.Random;
import java.util.Scanner; //import the scanner to display the text at the end

public class ChapterFivePractice { //Counting the number of conversations at Jeff Bezos Party.


public static void main(String[] args) { //method game picks a random number between 0 to 50

    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter the level of play. Press 1 for Hard, Press 2 for Easy, Press 3 for Medium: " );

int levelOfDifficulty = scan.nextInt();
        if (levelOfDifficulty == 1){
            getlevelHard();
        }
        if (levelOfDifficulty ==2){
            getlevelEasy();
        }
        if (levelOfDifficulty == 3){
            getlevelMedium();
        }
        else {
            System.out.print("invalid Entry");
        }

    while (correct == false) { //this will start our main algorithm letting the user know if their guess is correct
        if (guess == numberToGuess) {
        correct = true;
    }
        else if (guess < nummberToGuess) {
        System.out.println("your guess is too low");
        }
        else if (guess > numberToGuess) {
        System.out.println("your guess is too high");
        }
} //end of loop

System.out.println("you win!");
System.out.println("The number was " + numberToGuess);
System.out.println("It took you " + numberOfTries + " tries");

} // end of main method

   public static int getlevelHard() {
    int levelHard;
    levelHard = 1;
    while (numberOfTries<=3){
        numberOfTries++;
    return;
    }
}
public static int getlevelMedium() {
    int levelMedium;
    levelMedium = 3;
    while (numberOfTries<=6){
        numberOfTries++;
    return;
    }
}

public static int getlevelEasy() {
    int levelEasy;
    levelEasy = 2;
    while (numberOfTries<=10){
        numberOfTries++;
    return;
    }
}


} //end of main class

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner; //import the scanner to display the text at the end

public class ChapterFivePractice { // Counting the number of conversations at Jeff Bezos Party.

        public static void main(String[] args) { // method game picks a random number between 0 to 50

                Scanner scan = new Scanner(System.in);
                int retries = 0;

                while (retries == 0) {
                        System.out.println(
                                        "Please enter the level of play. Press 1 for Hard, Press 2 for Easy, Press 3 for Medium: ");

                        int levelOfDifficulty = scan.nextInt();
                        if (levelOfDifficulty == 1) {
                                retries = 3;
                        } else if (levelOfDifficulty == 2) {
                                retries = 6;
                        } else if (levelOfDifficulty == 3) {
                                retries = 10;
                        } else {
                                System.out.print("invalid Entry");
                        }
                }

                int attempts = 0;
                boolean correct = false;
                int numberToGuess = (int) (Math.random() * 50 + 1);
                while (!correct && attempts < retries) {
                        System.out.print("Enter your guess: ");
                        int guess = scan.nextInt();
                        if (guess == numberToGuess) {
                                correct = true;
                        } else if (guess < numberToGuess) {
                                System.out.println("your guess is too low");
                        } else if (guess > numberToGuess) {
                                System.out.println("your guess is too high");
                        }
                        attempts++;
                } // end of loop
                
                if(correct) {
                        System.out.println("you win!");
                        System.out.println("The number was " + numberToGuess);
                        System.out.println("It took you " + attempts + " tries");
                } else {
                        System.out.println("you lost!");
                        System.out.println("The number was " + numberToGuess);
                        System.out.println("you already took " + attempts + " attempts");
                }
                
                scan.close();
        } // end of main method

} // end of main class
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
This is a basic Java Question referencing Chapter 5 methods. The goal is to make a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

  • Original question IN JAVA Show the user a number of blank spaces equal to the number...

    Original question IN JAVA Show the user a number of blank spaces equal to the number of letters in the word For cat, you would show _ _ _. Prompt a user to guess a letter until they have run out of guesses or guessed the full word By default, the user should be able to make 7 failed guesses before they lose the game. If the user guesses a letter that is in the target word, it does not...

  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. The phrase is hidden-- all letters are replaced with asterisks. Spaces and punctuation are left unhidden. So if the phrase is "Joe Programmer", the initial partially hidden phrase is: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...

  • Java Listed below is code to play a guessing game. In the game, two players attempt...

    Java Listed below is code to play a guessing game. In the game, two players attempt to guess a number. Your task is to extend the program with objects that represent either a human player or a computer player. boolean checkForWin (int guess, int answer) { System.out.println("You guessed" + guess +"."); if (answer == guess) { System.out.println( "You're right! You win!") ; return true; } else if (answer < guess) System.out.println ("Your guess is too high.") ; else System.out.println ("Your...

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

  • java

    You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here.   Step 1Develop the following class:ClassName: CollegeDegreeAccess Modifier: publicInstance variablesName: majorAccess modifier: privateData type: StringName: numberOfCoursesAccess modifier: privateData type: intName: courseNameArrayAccess modifier: privateData type: String [ ]Name: courseCreditArrayAccess modifier: privateData type: int [ ]Static variablesName: maximumNumberOfCoursesAccess modifier: privateData type: intInitial Value: 40ConstructorName: CollegeDegreeAccess modifier: publicParameters: none...

  • /** * * Jumping frog game: user enters road length and series of jumps and *...

    /** * * Jumping frog game: user enters road length and series of jumps and * the program displays the current position of the frog. */ public class hw4_task5 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int playAgain = 1; int roadLen; System.out.println("JUMPING FROG"); while (playAgain == 1){ System.out.printf("Enter the length of the road: "); roadLen = in.nextInt(); play(roadLen); // Starts a new game with a road this long. System.out.printf("\nDo you want to play again...

  • (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming...

    (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming for code for the new GameChanger class. // the code for Main class for step number 6 has already been given, please show modified progrraming for Main class and GameCHanger class, thank you. package .games; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); Scanner sc = new Scanner(System.in); // Get upper...

  • Programming assignment for Java: Do not add any other instance variables to any class, but you...

    Programming assignment for Java: Do not add any other instance variables to any class, but you can create local variables in a method to accomplish tasks. Do not create any methods other than the ones listed below. Step 1 Develop the following class: Class Name: College Degree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String Name: courseCreditArray Access modifier:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT