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 the number in  + tries +  try.);                return true;            }        } else if (guess > randomNumber) {            System.out.println(Too high try again);            return false;        } else if (guess < randomNumber) {            System.out.println(Too low try again );            return false;        }        return false;    }    public void reset() {        this.tries = 0;        generateRandomNumber();    }}
Beginner number guessing game in Java
java;beginner;game;number guessing game
Unused imports - WarningsYou're using Eclipse. Eclipse and NetBeans are IDE's that will probably (aside from turning them off) give you warnings about your code.An unused import is such a thing.In Main, you don't use java.util.Random. So you should remove it.Your IDE will give you other warnings from time to time. If you look at the warnings it gives you, it will help in both debugging (hey, you're calling a function on a null variable here!) and in cleaning up your code (Use String.isEmpty() instead of string ==  - I know NetBeans even just allows you to click a button and it will fix it for you).Duplicate Code - RefactoringWhenever you can, try to not state things multiple times.Take this function.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 the number in  + tries +  try.);            return true;        }    } else if (guess > randomNumber) {        System.out.println(Too high try again);        return false;    } else if (guess < randomNumber) {        System.out.println(Too low try again );        return false;    }    return false;}What it's supposed to do:   Increase tries by 1. If the guess is correct, report the number of tries taken. If the guess is incorrect, report whether it is lower or higher than the hidden number. Return true if the guess was correct, and return false if the guess was incorrect.Now right now you're just comparing one int with another, so there's no reason to define guess == randomNumber as isGuessCorrect yet.What we can do, however, is remove duplicate code from branches.        if (tries > 1) {            System.out.println(You got the number in  + tries +  tries.);            return true;        } else {            System.out.println(You got the number in  + tries +  try.);            return true;        }That code can become this: 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;Another thing you could do, since it's a simple string, is to use a ternary:System.out.println(You got the number in  + tries + ((tries > 1) ?  tries. :  try.));return true;A similar thing can be done to the too high too low part.But here's another special case...If a number is not equal to and not lower to a certain value, then obviously it must be higher than that value.So the codeif(is equal){  print got number in x tries  return true} else if(is lower){  print try higher  return false} else if(is higher){  print try lower  return false}Could also beif(is equal){  print got number in x tries  return true} else if(is lower){  print try higher  return false} else {  print try lower  return false}Which would make the final snippet look something like this:public boolean guess(int guess) {    tries++;    if (guess == randomNumber) {        System.out.println(You got the number in  + tries + ((tries > 1) ?  tries. :  try.));        return true;    } //implicit else because return would end the function    if (guess > randomNumber) {        System.out.println(Too high try again);    } else {        System.out.println(Too low try again );    }    return false;}You might even want to consider exporting this whole message printing to separate functions:public boolean guess(int guess) {    tries++;    if (guess == randomNumber) {        printCorrectGuess(tries);        return true;    } //implicit else because return would end the function    if (guess > randomNumber) {        printTooHigh();    } else {        printTooLow();    }    return false;}private void printCorrectGuess(int tries){    System.out.println(You got the number in  + tries + ((tries > 1) ?  tries. :  try.));}private void printTooLow(){    System.out.println(Too low try again );}private void printTooHigh(){    System.out.println(Too high try again);}And (we can always go further) extracting the condition can make it easier to handle return values:public boolean guess(int guess) {    tries++;    boolean isGuessCorrect = guess == randomNumber;    if (isGuessCorrect) {        printCorrectGuess(tries);    } else if (guess > randomNumber) {        printTooHigh();    } else {        printTooLow();    }    return isGuessCorrect;}private void printCorrectGuess(int tries){    System.out.println(You got the number in  + tries + ((tries > 1) ?  tries. :  try.));}private void printTooLow(){    System.out.println(Too low try again );}private void printTooHigh(){    System.out.println(Too high try again);}Which turns a large function into several smaller functions, each easier to grasp than the original function you started with.