Heads or tails

The problem

Write a program that lets the user guess whether the flip of a coin results in heads or tails. The program randomly generates an integer 0 or 1, which represents head or tail. The program prompts the user to enter a guess and reports whether the guess is correct or incorrect. Feel free to include a delay to give the perception to the user that the program is generating a number.

Breaking it down

public static void main(String[] strings) {

    Scanner input = new Scanner(System.in);

    System.out.print("Enter 0 for heads or 1 for tails: ");
    int userGuess = input.nextInt();


    System.out.println("Flipping coin...");

    int coinSide = new Random().nextInt(2);

    if (userGuess == coinSide) {
        System.out.println("Good job! You guess is correct.");
    } else if (coinSide == 0) {
        System.out.println("Sorry, it is a heads");
    } else {
        System.out.println("Sorry, it is a tails");


Enter 0 for heads or 1 for tails: 1
Flipping coin...
Good job! You guess is correct.