#Code Review: Random Number Guess

1 messages · Page 1 of 1 (latest)

muted mantle
#

1/2

final class Game {
    static void gameLoop() {
        final Scanner stdin = new Scanner(System.in);
        final Random random = new Random();

        int minRange;
        int maxRange;
        int randNum;
        int randGuess;
        final int maxRetries = 3;
        int attempt = 0;
        String answer;

        while (true){
            try {
               do {
                    System.out.print("Enter the min number: ");
                    minRange = Integer.parseInt(stdin.nextLine());

                    if (minRange < 1 || minRange > 100){
                        System.out.print("The number you entered was less than 1 or greater than 100");
                    }
               }while (minRange < 1 || minRange > 100);

               break;
            }catch (NumberFormatException NFE){
                System.out.print("Invalid input was entered");
            }
        }

        while (true){
            try{
                do {
                    System.out.print("Enter the max number: ");
                    maxRange = Integer.parseInt(stdin.nextLine());

                    if (maxRange < minRange || maxRange > 100){
                        System.out.print("The number you entered was less than the min range");
                    }
                }while (maxRange < minRange || maxRange > 100);

                break;
            }catch (NumberFormatException NFE){
                System.out.print("Invalid input was entered");
            }
        }
karmic rootBOT
# muted mantle 1/2 ``` final class Game { static void gameLoop() { final Scanner s...

Detected code, here are some useful tools:

[WARNING] The code couldn't end properly...

Problematic source code:

final class Game {
    static void gameLoop() {
        final Scanner stdin = new Scanner(System.in);
        final Random random = new Random();

        int minRange;
        int maxRange;
        int randNum;
        int randGuess;
        final int maxRetries = 3;
        int attempt = 0;
        String answer;

        while (true){
            try {
               do {
                    System.out.print("Enter the min number: ");
                    minRange = Integer.parseInt(stdin.nextLine());

                    if (minRange < 1 || minRange > 100){
                        System.out.print("The number you entered was less than 1 or greater than 100");
                    }
               }while (minRange < 1 || minRange > 100);

               break;
            }catch (NumberFormatException NFE){
                System.out.print("Invalid input was entered");
            }
        }

        while (true){
            try{
                do {
                    System.out.print("Enter the max number: ");
                    maxRange = Integer.parseInt(stdin.nextLine());

                    if (maxRange < minRange || maxRange > 100){
                        System.out.print("The number you entered was less than the min range");
                    }
                }while (maxRange < minRange || maxRange > 100);

                break;
            }catch (NumberFormatException NFE){
                System.out.print("Invalid input was entered");
            }
        }```
Cause:
The code doesn't compile, there are syntax errors in this code.

## System out
[Nothing]
#

<@&987246399047479336> please have a look, thanks.

muted mantle
#

2/2

randNum = random.nextInt(minRange, maxRange);

        System.out.print("A random number has been picked from the range of " + minRange + " - " + maxRange + "\n");

        while (true){
            try {
                do {
                    System.out.print("Guess the number: ");
                    randGuess = Integer.parseInt(stdin.nextLine());

                    if (attempt == maxRetries) {
                        break;
                    }

                    if (randGuess < randNum){
                        System.out.println("Your guess was less than the random number. Try again");
                        attempt += 1;
                    } else if (randGuess > randNum) {
                        System.out.println("Your guess was greater than the random number. Try again");
                        attempt += 1;
                    } else {
                        System.out.println("The number you guessed was correct");
                    }
                }while (randGuess < randNum || randGuess > randNum);

                break;
            }catch (NumberFormatException NFE){
                System.out.print("Invalid input was entered");
            }
        }

        System.out.print("The random number was " + randNum + ". Enter another number range?: ");
        answer = stdin.nextLine().toLowerCase();

        while (true){
            switch (answer) {
                case "y" -> gameLoop();
                case "n" -> stdin.close();
                case "" -> {
                    System.out.print("Empty input was entered. Enter another number range?: ");
                    answer = stdin.nextLine().toLowerCase();
                }
                default -> {
                    System.out.print("Invalid input was entered. Enter another number range?: ");
                    answer = stdin.nextLine().toLowerCase();
                }
            }
        }
    }
}
karmic rootBOT
muted mantle
#

couldnt put the whole thing without splitting it up

brisk citrus
#

u can drag drop the file and our bot makes it nice 🙂

muted mantle
#

the .java file?

brisk citrus
#

for example, yes

muted mantle
karmic rootBOT
# muted mantle

I uploaded your attachments as Gist. This makes them more accessible, for example to mobile users.

muted mantle
#

epic

modest pike
#

You should set MaxRetries to a reasonable number based on range of numbers.

#

Also, nextInt(origin, bound), like most things in Java, the start of the range is inclusive and the end of the range is exclusive, so to get that end range to be inclusive, you have to add 1 to it.

muted mantle
#

but how is the code written?

modest pike
#

In your code you have what is referred to as 'magic numbers'.
final int maxRetries = 3;
minRange < 1 || minRange > 100

#

When people (aka, future you) look at this code they will say "why 3" or "why is 100 repeated everywhere"

#

So if there was a constant to represent the top end of the guessing range, you'd put it in a final int with a ALL UPPERCASE variable name like final int RANGE_TOP = 100

#

all uppercase variable names usually represent constants.

#

Then when you need to refer to that in your if statements or printlns, you refer to the variable RANGE_TOP instead of some explicit number like 100.

#

If you do this then the effort to change it through out your code from 100 to 100,000 is just to change the final int RANGE_TOP = 100_000;

#

random.nextInt(minRange, maxRange+1);

#

is what you need to get between a number between 1 - 100, not 1 - 99.

#

MaxRetries can be set after you know the minRange and maxRange chosen by the player.

#

Think about the logical number of tries needed for any range of numbers. If you are guessing and getting feedback of higher/lower then you can think about it as eliminating half(on averarge) the possible choices each time.

karmic rootBOT
#

@muted mantle

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍