One-shot number guessing game This program is one of the exercises I solved in my book. Can anyone suggest any improvements to it?

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

public class GuessMyNumber {

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Random random = new Random();

    System.out.println("I'm thinking of a number between 1 and 100 (including both).\nCan you guess what it is?");
    System.out.print("Type a number : ");
    int guessNum = in.nextInt();
    System.out.printf("Your guess is : %d\n", guessNum);
    int randomNum = random.nextInt(100) + 1;
    System.out.printf("The number I was thinking of is : %d\n", randomNum);
    System.out.print("You were off by : ");

    int diff = guessNum - randomNum;
    if (guessNum &gt;= randomNum) {
      System.out.println(diff);
    } else { System.out.println(randomNum - guessNum); }
  }
}
int diff = guessNum - randomNum;
    if (guessNum &gt;= randomNum) {
      System.out.println(diff);
    } else { System.out.println(randomNum - guessNum); }



You mix styles here.  Consider 

    if (guessNum &gt;= randomNum) { System.out.println(guessNum - randomNum); }
    else { System.out.println(randomNum - guessNum); }


That's a distinct style.  It puts the {} on the same line as the contained statement and it does every calculation just when it is needed.  Most importantly it's consistent.  

I don't like that style.  It's especially uncommon.  I'd prefer 

    int difference = guess - solution;
    if (difference &gt;= 0) {
        System.out.println(difference + " high.");
    } else {
        System.out.println(-difference + " low.");
    }


We only calculate the difference once.  Then we use it three times.  

This uses a four-column indent, which is standard in Java.  

This puts {} on the same lines as their control structures when possible.  And it puts the enclosed statements on lines of their own.  Also standard in Java.  

I changed the names of the variables to concentrate on their role in the program.  Not characteristics of how they are defined, e.g. randomly or numeric.  This leaves us with more flexibility to change in the future.  For example, this version could be easily modified to guess letters.  

If you don't like the high/low, you could just say 

    System.out.println(Math.abs(guessNum - randomNum));


No intermediate variables or if/else that way.