Tuesday, March 14, 2017

// // 1 comment

Exercise 3.14 — Game: Heads or Tails

Java 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.

 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com

import java.util.Scanner;

public class ComputeAndInterpretBMI {
    public static void main (String [] args) {
        int number, guess;

        // Obtain the random number 0 or 1  
        number = (int) (Math.random() * 2);
        // Prompt the user to enter a guess
        Scanner input = new Scanner(System.in);

        System.out.print("Guess head or tail? Enter 0 for head and 1 for tail: ");
        guess = input.nextInt();

        // Check the guess
        if (guess == number) {
            System.out.println("Correct guess");
        } else {
            System.out.println("Wrong guess");

Liang Introduction to Java Programming exercise 3.14 solution
Program sample run

Click here to see other solutions to Introduction to Java Programming.

1 comment:

  1. Here is my solution: https://github.com/mertserezli/ProgrammingChallenges/tree/master/01-HeadsTails/src/com/company


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately. To get notified of replies or follow-up comments, click the box next to notify me.