This page provides a line-by-line overview of the code used in the GuessIt Java program. I hope this can be used as a semi-purposeful demonstration as to how to do loops, if statements and use variables in Java.
Before reading through "The Code" section below, I recommend you take a look at the program flow diagram below, and then have a nosey through the guessit.java file and give it a run (see compiling it below). See if you can understand it without an explanation.
To compile the program run the command 'javac guessit.java' and to run it type 'java guessit'. The SavitchIn class is already compiled for you.
Below is how the program should flow, it's not very advanced so shouldn't be too hard to understand.

Got it? We pick a random number at the start and, ask the user to guess it. We then stay in a loop until the user guesses the right number or types 0, telling them to guess higher or lower with each guess.
Once they've got the right number or typed 0, we exit the loop, check they did get the right answer and tell them so.
If you haven't already, you should download java_guessit.zip to view the code. The code is heavily commented, we'll skip the comments here as we're covering it in more detail.
The program code starts on line 17, the import command is beyond the scope of this explanation, but it is required in order to generate the random number later in the program.
We start the body of the program on line 19, you should be reasonably familiar with this, the main function starts on line 21. Now onto the program itself.
On line 24 we define three variables of type int. We assign the value 0 to the count variable. These should be self-explanatory, the random_no variable will hold the randomly generated number between 1 and 100, the last_guess variable will hold the user's input, and the count will keep count of their attempts.
int random_no, last_guess, count=0;
Lines 30 and 31 generate the random number and store it in the random_no variable, again I'm not covering packages etc. here, you just need to know that after these two lines random_no will hold an integer between 1 and 100.
Random generator = new Random();
random_no = generator.nextInt(99) + 1;
Lines 34 to 36 tell the user what's happening by printing some output to the screen.
System.out.print("I've picked a number between 1 and 100, take a ");
System.out.println("guess at what it is.");
System.out.println("Type 0 to give up (lame).");
do
{
We start our loop on line 39. I'm using a do loop because it will need to be executed at least once. The first thing we do in the loop is to get the input from the user and store it in last_guess. We then increment the count variable by 1.
last_guess = SavitchIn.readLineInt();
count++;
We now need to check whether the number inputted (last_guess) is higher or lower than the random number (random_no). We use an if statement for this, on the basis that if the number is lower, tell them to guess higher, if it's not lower then it must be equal to or higher, so check if it is higher and if it is, tell them to guess lower. We don't need to do anything at this stage if it's equal to.
if (last_guess < random_no)
System.out.println("My number is higher");
else if (last_guess > random_no)
System.out.println("My number is lower");
The last action for the loop is to check whether we need to stay in it. We want to stay in the loop if the user has guessed wrong and hasn't typed in 0.
} while ((random_no != last_guess) && (last_guess != 0));
Once the random_no variable is equal to the last_guess variable, or last_guess is equal to 0 we exit the loop. The only thing left to do is determine whether we came out of the loop because the user quit (typed 0) or guessed the right number. So we use an if statement to check and print the number of goes.
if (last_guess != 0)
System.out.println("You got it in " + count + " attempts.");
The only flaw in this program is that we do not check whether the user typed 0 before telling them to guess higher or lower, so when a user exits by guessing 0 they are told to guess higher.
This can be fixed by adding an extra condition to lines 47 and 49, I left them out to help keep the code more easily readable.
I've enabled comments below so you can leave me feedback, I'd like to know if this was helpful, clear and easy to understand, or completely the opposite, constructive critism is more than welcome.