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

Beginner number guessing game in Java

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

Problem

I am a first year CS student. We are currently learning Java and my latest assignment was to create this random number game. I am hoping to get some feedback on code style etc so that I know what not to do in the future and so that I can avoid getting into bad habits from the start. I don't know all that much so any explanations/tips at all will be amazing!

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

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean win = false;
        boolean running = true;
        System.out.println("Please enter the upper limit: ");
        Game game = new Game(scan.nextInt());

        while (running) {

            System.out.println("Please enter a guess ");

            while (!win) {
                if (game.guess(scan.nextInt())) {
                    win = true;

                }
            }
            System.out.println("Would you like to play again y/n?");
            String playAgain = scan.next();
            if (playAgain.equalsIgnoreCase("y")) {
                game.reset();
                running = true;
                win = false;
            } else {
                running = false;
                System.out.println("Game over");
            }
        }
    }
}


Game class:

```
import java.util.Random;

public class Game {

private int randomNumber;
private int tries = 0;
private int range;

public Game(int range) {
this.range = range;
generateRandomNumber();
}

private void generateRandomNumber() {
Random rand = new Random();
this.randomNumber = rand.nextInt(range) + 1;
}

public boolean guess(int guess) {
tries++;
if (guess == randomNumber) {
if (tries > 1) {
System.out.println("You got the number in " + tries + " tries.");
return true;
} else {
System.out.println("You got th

Solution

This is a pretty good implementation of what you've set out to do. The naming of things is clear and the code is pretty well split up into methods.

Main Class

I'd maybe put a slightly clearer explanation of what the user is expected to do at the start, as well as your "enter an upper limit". This is usually an excuse to break out your finest ascii art.

Something else to consider is what if your user doesn't enter an integer? That will break your program, so putting in some error handling that asks your error-prone meatbag to try again and get it right this time, please. Your way of checking is to put the scan.nextInt() in a try/catch block and catch an InputMismatchException.

When you're asking the player if they want to play again, you set running to true, but there's no way for it not to be true, so that's unnecessary.

Game class

You're seeding rand every time you're calling generateRandomNumber(), which isn't really going to be an issue here, as you're not calling that method quickly/often, but you should really only seed a random number generator once in a program, otherwise you'll start having it return the same value over and over again, so maybe move your Random rand = new Random() to the constructor?

In guess(), I'd rearrange the first section to this:

if (guess == randomNumber) {
        if (tries > 1) {
            System.out.println("You got the number in " + tries + " tries.");
        } else {
            System.out.println("You got the number in " + tries + " try.");
        }
        return true;
 }


You're repeating yourself less and the return value is the same regardless. The same goes for the next part, instead, I'd have:

else if (guess > randomNumber) {
        System.out.println("Too high try again");
    } else if (guess < randomNumber) {
        System.out.println("Too low try again ");
    }
    return false;

Code Snippets

if (guess == randomNumber) {
        if (tries > 1) {
            System.out.println("You got the number in " + tries + " tries.");
        } else {
            System.out.println("You got the number in " + tries + " try.");
        }
        return true;
 }
else if (guess > randomNumber) {
        System.out.println("Too high try again");
    } else if (guess < randomNumber) {
        System.out.println("Too low try again ");
    }
    return false;

Context

StackExchange Code Review Q#68358, answer score: 15

Revisions (0)

No revisions yet.