HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Guess a random number between a selected interval

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
randomnumberintervalbetweenselectedguess

Problem

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!!");

Solution

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!

Code Snippets

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

Context

StackExchange Code Review Q#36074, answer score: 12

Revisions (0)

No revisions yet.