Hi-Lo guessing game with numbers I am new to Java in general, but wanted to get some input on the code for a CS class I am taking.  It is a program that plays the Hi-Lo guessing game with numbers. A user may choose to quit mid-game by entering 0 and then they are prompted to play again if they choose.  If they guess correctly, they are also prompted to play again.

The code works fine, however, my original direction was to use the while loop as the main conditional instead of the do statement but I couldn't get the game to start again after the user entered 0 to exit the loop. I'm curious to know how I can use the while loop as the main conditional vs the do statement.

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

public class HiLo {
    public static void main(String[] args)
    {
        int guess, answer, count = 0;
        String restart;

        Random generator = new Random();
        Scanner scan = new Scanner(System.in);

        System.out.print("What number do you guess (enter 0 to quit)? ");
        guess = scan.nextInt();

        answer = generator.nextInt(100) + 1; // randomly generates a number between 1-100

        do // loops through game until user gets answer correct or quits
        {
            count++;

            if (guess == 0) // if user enters 0 to quit game, this message is displayed
            {
                System.out.println("\nQuitting game");
                System.out.println("\nWould you like to play again?");
                restart = scan.next();
                if (restart.equalsIgnoreCase("Y"))
                {
                    System.out.print("Enter a number to start playing");
                    guess = scan.nextInt();
                }
                else
                    break;
            }
            if (guess == answer)
            {
                System.out.println("That's correct! You made " + count + " guesses.");
                System.out.println("Would you like to play again?");
                restart = scan.next();
                if (restart.equalsIgnoreCase("Y"))
                {
                    System.out.print("Enter a number to start playing");
                    guess = scan.nextInt();
                }
                else
                    break;
            }
            if (guess &lt; answer)
            {
                System.out.print("Your guess is too low, try again.");
                guess = scan.nextInt();
            }
            if (guess &gt; answer)
            {
                System.out.print("Your guess is too high, try again.");
                guess = scan.nextInt();
            }

        }
        while (guess != 0);

        System.out.print("Thanks for playing!");
    }
}
Don't repeat yourself

You twice say 


                System.out.println("\nWould you like to play again?");
                restart = scan.next();
                if (restart.equalsIgnoreCase("Y"))
                {
                    System.out.print("Enter a number to start playing");
                    guess = scan.nextInt();
                }
                else
                    break;



And five times say 


                    guess = scan.nextInt();



Including twice in the previous repeated code.  Don't do that.  Instead organize the code so that you say these things once each.  

Consider the following 

        String restart = "Y";
        while (restart.equalsIgnoreCase("Y")) {
            int answer = generator.nextInt(100) + 1;

            System.out.print("What number do you guess (enter 0 to quit)? ");
            while (true) {
                int guess = scan.nextInt();
                if (guess == 0) {
                    System.out.println("\nQuitting game");
                    break;
                }

                count++;
                if (guess == answer) {
                    System.out.println("That's correct! You made " + count + " guesses.");
                    break;
                }

                if (guess &lt; answer) {
                    System.out.println("Your guess is too low, try again.");
                } else {
                    System.out.println("Your guess is too high, try again.");
                }
            }

            System.out.println("\nWould you like to play again (Y/N)?");
            restart = scan.next();
        }


This won't necessarily work any better.  But this pattern will generally be easier to maintain.  One of the problems with academic code is its low reuse.  You never have to maintain it, so it's easy to slip into unmaintainable habits.  Outside the academic context, most of your time will be spent on maintaining existing code.  

I used a while for the outer loop even though a do/while would have been more natural (since you always loop once).  Notice though that it is gated by restart rather than guess.  I find this to be a cheesy solution and would prefer the do/while.  

The inner loop will loop forever until explicitly exited (with the break statements).  Notice how the first thing that it does is collect the next guess.  This saves collecting it in the five different places.  Collect it once and then process it once.  

When processing guess, you don't need to do three comparisons.  There are only three states.  If it's not equal (otherwise we break out of the loop) and not less than, it must be greater than.  So use an else rather than the third comparison.  

We could also use an else if after guess == answer, but we don't need to do so since we break.  

I added a (Y/N) to your play again query.  As is, it works if you know how.  But someone who doesn't know what to do might type "Yes." which seems like a reasonable response but won't work.  Give a little extra guidance as to what to type there.  You also might consider accepting "Yes" as a positive answer.  

The Java standard is to put { on the same line as a method declaration or control structure, so I switched to that.  It's more important to be consist though.  So the other way is OK.  It's just not how I normally write Java.  

Bugs

You don't manage the count correctly.  I fixed a small mistake for you, but I left the bigger one.  The small mistake was that you incremented the count even if the guess was 0, which is not a real guess but the exit criterion.  Without the larger mistake, this doesn't really matter.  You don't display the incorrect count anyway.  

The larger mistake comes when you play repeated games.  To my mind, the correct way to fix this would be to move the count declaration and initialization to a different location.  Note that in Java, variable declarations do not need to be at the beginning of a block.  They can appear anywhere.  Figure out what's wrong with the current behavior (win repeated games until it becomes obvious).  Then think about where it should be.  

Use methods

I would prefer to move generator and scan into class fields.  This means inside the class but outside any method.  

    private static final Random GENERATOR = new Random();
    private static final Scanner SCAN = new Scanner(System.in);


That would allow them to be shared across multiple methods.  

I would expect main to look more like 

    public static void main(String[] args) {
        String restart;
        do {
            play();

            System.out.println("Would you like to play again (Y/N)?");
            restart = SCAN.next();
        } while (restart.equalsIgnoreCase("Y"));
    }


Perhaps you haven't gotten to methods yet.  When you do, consider going back to this and writing a play method.