-
Notifications
You must be signed in to change notification settings - Fork 14
/
headsTails3524.java
39 lines (30 loc) · 1.37 KB
/
headsTails3524.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
31
32
33
34
35
36
37
38
39
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;
public class headsTails3524 {
public static void main(String[] args) {
System.out.println("2021503524 - Mugundh J B");
System.out.println("Date: " + LocalDate.now() + " Time: " + LocalTime.now());
// Create a random number generator
Random rand = new Random();
// Generate a random number (0 or 1)
int num = rand.nextInt(2);
// Create a Scanner to read user input
Scanner in = new Scanner(System.in);
// Prompt the user for their choice
System.out.print("Enter your choice (h / H / t / T): ");
char choice = in.next().charAt(0);
// Validate user's choice
while (choice != 'h' && choice != 'H' && choice != 't' && choice != 'T') {
System.out.printf("Invalid choice. Enter your choice (h / H / t / T): ");
choice = in.next().charAt(0);
}
// Display the random value generated
System.out.printf("Random value = %d\n", num);
// Check if the user won or lost based on their choice and the random value
if (((choice == 'h' || choice == 'H') && num == 1) || ((choice == 't' || choice == 'T') && num == 0))
System.out.print("You won!");
else
System.out.print("You lose!");
}
}