forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExercise03_14.java
30 lines (23 loc) · 833 Bytes
/
Exercise03_14.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package ch_03;
import java.util.Scanner;
/**
* 3.14 (Game: heads or tails) 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.
*/
public class Exercise03_14 {
public static void main(String[] args) {
int x = (int) (Math.random() * 2);
System.out.println("Enter your guess now! 0 for heads, or 1 for tails:");
Scanner input = new Scanner(System.in);
int a = input.nextInt();
if (a == x) {
System.out.println("You are correct!");
} else {
System.out.println("Wrong! Better luck next time!");
System.out.println("The correct anwser was: " + x);
}
input.close();
}
}