10
\$\begingroup\$

My project for my class is to create a Java-based game where the user must enter a number between 1-20 and a number between 250 and 300. The computer randomly chooses a number between those 2 numbers. Then the user has 10 guesses to correctly guess what number the computer is "thinking of."

There is a catch, though. We cannot use while loops, only if and else-if statements. I have started this code and was wondering if I'm on the right track. Please point out anything that might help me!

package guessthenumber;

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

public class GuessTheNumber {

    public static void main(String[] args) 
    {
        int lowBoundary, highBoundary, secretNumber, boundaryDifference,
                g1, g2, g3, g4, g5, g6, g7, g8, 
                g9, g10;
        //g1, g2,... = guess 1, guess2,...

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Can you guess the number I'm thinking of?\nLet's "
                + "see if you can guess the right number within 10 guesses.\n\n"
                + "First please enter a number between 1 and 20.");
        lowBoundary = keyboard.nextInt();

        System.out.println("Excellent! Next enter a number between 250 and "
                + "350.");
        highBoundary = keyboard.nextInt();

        boundaryDifference = highBoundary - lowBoundary;
        Random randomNumber = new Random();
        secretNumber = randomNumber.nextInt(boundaryDifference) + lowBoundary;

        System.out.println("The secret number has been chosen. Now you must "
                + "guess\nthe secret number within 10 guesses or else you lose."
                + "\n(Hint: The secret number is between " + lowBoundary +
                " and " + highBoundary + ".)");
        g1 = keyboard.nextInt();

        if (g1 == secretNumber) {
            System.out.println("CONGRATULATIONS! YOU'VE CORRECTLY GUESSED\nTHE"
                    + "SECRET NUMBER ON YOUR FIRST GUESS!!");
        } 

        else if (g1 < secretNumber) {
            System.out.println("Higher than " + g1 + ". Guess again!");
            g2 = keyboard.nextInt();

            {
                if (g2 == secretNumber) {
                    System.out.println("Awesome! You've correctly guessed\nthe"
                            + "secret number in 2 guesses!");
                }

                else if (g2 < secretNumber) {
                    System.out.println("Higher than " + g2 + ". Guess again!");
                    g3 = keyboard.nextInt();

                    if (g3 == secretNumber) {
                    System.out.println("Great job! You've correctly guessed\n"
                            + "the secert number in 3 guesses!");
                }
                else if (g3 < secretNumber) {
                    System.out.println("Higher than " + g3 + ". Guess again!");
                    g4 = keyboard.nextInt();

                    if (g4 == secretNumber) {
                    System.out.println("Good job! You've correctly guessed\nthe"
                                + " secret number in 4 guesses!");
                    }

                    else if (g4 < secretNumber) {
                    System.out.println("Higher than " + g4 + ". Guess again!");
                    g5 = keyboard.nextInt();

                    if (g5 == secretNumber) {
                    System.out.println("Not bad! You've correctly guessed\nthe"
                            + " secret number in 5 guesses!");
                    }

                    else if (g4 > secretNumber) {
                    System.out.println("Lower than " + g4 + ". Guess again!");
                    }
                    }
                    }

                else if (g3 > secretNumber) {
                    System.out.println("Lower than " + g3 + ". Guess again!");
                }
                }

                else if (g2 > secretNumber) {
                    System.out.println("Lower than " + g2 + ". Guess again!"
                            );
                    g3 = keyboard.nextInt();
                }
            }
        }

        else if (g1 > secretNumber) {
            System.out.println("Lower than" + g1 + ". Guess again!");

            {

            }
        }
    }
}
\$\endgroup\$
12
\$\begingroup\$

I'm guessing you're not allowed to use any kind of loop, because then you could just use a for-loop.

Anyhow, I would suggest you encapsulate more of your code in methods. Think about what kind of operations you use repeatedly.

Also, you don't need multiple guessing variables (g1, g2, g3, ... , g10); you can make do with one.

Now to solve the main problem. I would suggest using a recursive method (a method that calls for itself). It can very well act as a loop and you could, for example, count the guesses with it among other things.

Here's an example of a recursive method that returns the sum of all integers from 1 up to a given number:

public static int sumIntegers(int number){
    if (number == 1)
        return number;
    return number + sumIntegers(number - 1);
}

If you would call this method like this:

sumIntegers(5);

You would get: 5 + 4 + 3 + 2 + 1 = 15

Hope it helps!

\$\endgroup\$
  • 2
    \$\begingroup\$ +1 for giving a push in the right direction without revealing too much (considering the homework tag on the question) \$\endgroup\$ – Simon Forsberg Nov 25 '13 at 21:12
10
\$\begingroup\$

This code is somewhat broken (slightly):

    boundaryDifference = highBoundary - lowBoundary;
    Random randomNumber = new Random();
    secretNumber = randomNumber.nextInt(boundaryDifference) + lowBoundary;

This is one of the most common duplicate problems on StackOverflow, and it's a lesson you should just learn....

The above code will never produce the value 'highBoundary`.

This is because nextInt(boundaryDifference) produces a result from 0 to boundaryDistance excluding boundaryDistance.

The right way to get a random number from a given range including both limits of the range is:

 int val = min + randomNumber.nextInt((max - min) + 1);

Never forget the +1

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy