-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeadOrTail.java
More file actions
39 lines (37 loc) · 1.24 KB
/
HeadOrTail.java
File metadata and controls
39 lines (37 loc) · 1.24 KB
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 com.sun.source.util.TaskListener;
import java.util.Random;
import java.util.Scanner;
public class HeadOrTail {
private static int range = 2;
public static int headsCounter = 0;
public static int tailsCounter = 0;
static public String name;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
int key;
System.out.println("Who are you?");
System.out.print("> ");
name = sc.next();
System.out.println("Hellow, " + name + "!");
System.out.println("Tossing a coin...");
for (int i = 1; i < 4; i++){
System.out.print("Round " + i + ": ");
key = rnd.nextInt(range);
if (key == 1){
System.out.println("Head");
headsCounter++;
}else
{
System.out.println("Tails");
tailsCounter++;
}
}
System.out.println("Heads: " + headsCounter + ", Tails: " + tailsCounter);
if (headsCounter > tailsCounter){
System.out.println(name + " won!");
}else {
System.out.println(name + " lost.");
}
}
}