Question #47620

Part I (Chapter 4 & 5): Guess a number from 1 to 100
Example of Run-time Output Console
Welcome to the Guess the Number Game
++++++++++++++++++++++++++++++++++++

I'm thinking of a number from 1 to 100.
Try to guess it.

Enter number: 50
You got it in 1 tries.
Great work! You are a mathematical wizard.

Try again? (y/n): y

I'm thinking of a number from 1 to 100.
Try to guess it.

Enter number: 50
Way too high! Guess again.

Enter number: 30
Too high! Guess again.

Enter number: 15
Too low! Guess again.

Enter number: 23
Too high! Guess again.

Enter number: 19
Too low! Guess again.

Enter number: 21
Too high! Guess again.

Enter number: 20
You got it in 7 tries.
Not too bad! You've got some potential.

Try again? (y/n):
Error! This entry is required. Try again.
Try again? (y/n): x
Error! Entry must be 'y' or 'n'. Try again.
Try again? (y/n): n

Bye - Come back soon!

Press any key to continue . . .
Operation
• Write a class called GuessGame with all business logics for guess game, Use the Validator class in
1

Expert's answer

2014-10-08T00:49:01-0400

Answer on Question #47620, Programming, Java | JSP | JSF

Problem.

Part I (Chapter 4 & 5): Guess a number from 1 to 100

Example of Run-time Output Console

Welcome to the Guess the Number Game

++++++++++++++++++++++++++++++++++++++++++++

I'm thinking of a number from 1 to 100.

Try to guess it.

Enter number: 50

You got it in 1 tries.

Great work! You are a mathematical wizard.

Try again? (y/n): y

I'm thinking of a number from 1 to 100.

Try to guess it.

Enter number: 50

Way too high! Guess again.

Enter number: 30

Too high! Guess again.

Enter number: 15

Too low! Guess again.

Enter number: 23

Too high! Guess again.

Enter number: 19

Too low! Guess again.

Enter number: 21

Too high! Guess again.

Enter number: 20

You got it in 7 tries.

Not too bad! You've got some potential.

Try again? (y/n):

Error! This entry is required. Try again.

Try again? (y/n): x

Error! Entry must be 'y' or 'n'. Try again.

Try again? (y/n): n

Bye - Come back soon!

Press any key to continue . . .

Operation

- Write a class called GuessGame with all business logics for guess game, Use the Validator class in

Solution.

Code (GuessGame.java)


import java.util.Random;
import java.util.Scanner;
public class GuessGame {
    private Scanner myScanner = new Scanner(System.in);
    /**
     * Reads user input.
     *
     * @return user input.
     */
    private int getNumber() {
        int number = 0;
        System.out.print("Enter number: ");
        if (myScanner.hasNextInt()) {
            number = myScanner.nextInt();
        } else {
            while (!myScanner.hasNextInt()) {
                myScanner.next();
                System.out.println("Incorrect input!\n");
                System.out.print("Enter number: ");
                if (myScanner.hasNextInt()) {
                    number = myScanner.nextInt();
                    break;
                }
            }
        }
        return number;
    }
    /**
     * Starts guess game.
     */
    public void startGame() {
        // Random number generating
        Random rand = new Random();
        int numberToGuess = rand.nextInt(100) + 1;
        System.out.println("I'm thinking of a number from 1 to 100.\n" +
                "Try to guess it.");
        int enteredNumber = 0;
        int numberOfGuessings = 0;
        // Guess
        while (numberToGuess != enteredNumber) {
            enteredNumber = getNumber();
            numberOfGuessings++;
            if (enteredNumber == numberToGuess) {
                System.out.println("You got it in " +
                        Integer.toString(numberOfGuessings) + " tries.");
            } else if (enteredNumber > numberToGuess) {
                System.out.println("Too high! Guess again.\n");
            } else {
                System.out.println("Too low! Guess again.\n");
            }
        }
        if (numberOfGuessings == 1) {
            System.out.println("Great work! You are a mathematical wizard.\n");
        } else {
            System.out.println("Not too bad! You've got some potential.\n");
        }
    }
}


Code (Main.java)


import java.util.Scanner;
public class Main {
    private static Scanner myScanner = new Scanner(System.in);
    public static void main(String[] args) {
        GuessGame game = new GuessGame();
        String choice = "y";
        while (choice.contentEquals("y")) {
            game.startGame();
            System.out.print("Try again? (y/n): ");
            choice = myScanner.nextLine();
            while (!choice.contentEquals("y") && !choice.contentEquals("n")) {
                System.out.println("Error! This entry is required. Try again.");
                System.out.print("Try again? (y/n): ");
                choice = myScanner.nextLine();
            }
            System.out.println();
        }
    }
}


Result

I'm thinking of a number from 1 to 100.

Try to guess it.

Enter number: 50

Too low! Guess again.

Enter number: 75

Too high! Guess again.

Enter number: 63

Too high! Guess again.

Enter number: 57

You got it in 4 tries.

Not too bad! You've got some potential.

Try again? (y/n): y

I'm thinking of a number from 1 to 100.

Try to guess it.

Enter number: 50

Too high! Guess again.

Enter number: 25

Too high! Guess again.

Enter number: aa

Incorrect input!

Enter number: 10

Too low! Guess again.

Enter number: 25

Too high! Guess again.

Enter number: 15

Too high! Guess again.

Enter number: 13

Too low! Guess again.

Enter number: 12

Too low! Guess again.

Enter number: 11

Too low! Guess again.

Enter number: 14

You got it in 9 tries.

Not too bad! You've got some potential.

Try again? (y/n): n

http://www.AssignmentExpert.com/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS