-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOddsAndEvensGame.java
More file actions
67 lines (61 loc) · 2.5 KB
/
OddsAndEvensGame.java
File metadata and controls
67 lines (61 loc) · 2.5 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package MyJavaProject;
import java.util.*;
public class OddsAndEvensGame {
public static void main(String[] args) {
System.out.println("Let's play a game called \"OddsAndEvens\".");
Scanner scan = new Scanner(System.in);
System.out.print("What is your userName?");
String userName = scan.next();
System.out.println("Hi " + userName + ", which do you choose? (O)dds or (E)Evens ?");
Scanner scan2 = new Scanner(System.in);
String str = scan2.next();
str = str.toUpperCase();
String odd = "O";
String even = "E";
if (str.equals(odd)) {
System.out.println(userName + " has picked " + str + ". The computer will be " + even + ".");
System.out.println("Let's have fun !!");
} else if (str.equals(even)) {
System.out.println(userName + " has picked " + str + ". The computer will be " + odd + ".");
System.out.println("Let's have fun !!");
} else {
System.out.println("Choose between O or E please");
}
System.out.println("------------------------------------------------------------------");
//Part 2
System.out.println("How many \" fingers\" do you put out? ");
Scanner scan3 = new Scanner(System.in);
int userNumber = scan3.nextInt();
Random rand = new Random();
int computerNumber = rand.nextInt(6);
System.out.println("The computer plays number " + computerNumber + " \"fingers\"");
System.out.println("------------------------------------------------------------------");
int sum = userNumber + computerNumber;
System.out.println("The sum is: " + sum);
/* if(sum%2 == 0 ){
System.out.println( "Bravo ! Even win !");
}
else{
System.out.println("Bravo ! Odds wins");
}*/
// or
boolean OddOrEven = sum % 2 == 0;
if (OddOrEven) {
System.out.println("The sum is Even");
if (str.toUpperCase().equals(even)) {
System.out.println("The winner is " + userName);
} else {
System.out.println("The winner is the computer");
}
}
else{
System.out.println("The sum is odd");
if (str.toUpperCase().equals(odd)){
System.out.println("The winner is "+userName);
}
else {
System.out.println("The winner is the computer");
}
}
}
}