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.
Click here to see other solutions to Introduction to Java Programming.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> */<br /><br />import java.util.Scanner;<br /><br />public class ComputeAndInterpretBMI {<br /> public static void main (String [] args) {<br /> int number, guess;<br /><br /> // Obtain the random number 0 or 1 <br /> number = (int) (Math.random() * 2);<br /> // Prompt the user to enter a guess<br /> Scanner input = new Scanner(System.in);<br /><br /> System.out.print("Guess head or tail? Enter 0 for head and 1 for tail: ");<br /> guess = input.nextInt();<br /><br /> // Check the guess<br /> if (guess == number) {<br /> System.out.println("Correct guess");<br /> } else {<br /> System.out.println("Wrong guess");<br /> }<br /> }<br />}<br /> |
1 2 |
<br /> |
Program sample run |
1 2 |
<br /> |
Leave A Comment