Skip to content

Commit 3113d79

Browse files
Exercise 2
1 parent 38fe760 commit 3113d79

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

20.Java_Exercise_2/RPS.java

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.util.*;
2+
import java.util.Random;
3+
4+
public class RPS {
5+
public static void main(String args[]) {
6+
Scanner sc = new Scanner(System.in);
7+
Random random = new Random();
8+
9+
int cpu_score = 0;
10+
int player_score = 0;
11+
12+
while (true) {
13+
14+
int computer = random.nextInt(3);
15+
16+
String cpu_choice = "";
17+
18+
if (computer == 0) {
19+
cpu_choice += "rock";
20+
}
21+
else if (computer == 1) {
22+
cpu_choice += "paper";
23+
}
24+
else {
25+
cpu_choice += "scissor";
26+
}
27+
28+
System.out.println("Please enter your choice");
29+
System.out.println("Enter rock || paper || scissor || end ||");
30+
String player = sc.next();
31+
32+
if (player.equals(cpu_choice)) {
33+
System.out.println("It's Draw");
34+
}
35+
36+
else if (player.equals("rock")) {
37+
if (cpu_choice.equals("paper")) {
38+
System.out.println("CPU Wins...");
39+
cpu_score += 1;
40+
}
41+
else {
42+
System.out.println("Player Wins...");
43+
player_score += 1;
44+
}
45+
}
46+
47+
else if (player.equals("paper")) {
48+
if (cpu_choice.equals("rock")) {
49+
System.out.println("Player Wins...");
50+
player_score += 1;
51+
}
52+
else {
53+
System.out.println("CPU Wins...");
54+
cpu_score += 1;
55+
}
56+
}
57+
58+
else if (player.equals("scissor")) {
59+
if (cpu_choice.equals("paper")) {
60+
System.out.println("Player Wins...");
61+
player_score += 1;
62+
}
63+
else {
64+
System.out.println("CPU Wins...");
65+
cpu_score += 1;
66+
}
67+
}
68+
else if (player.equals("end")) {
69+
System.out.println("The final scores are as follows : ");
70+
System.out.println("CPU Score : "+ cpu_score);
71+
System.out.println("Player Score : " + player_score);
72+
System.out.println("Thanks for playing my game.");
73+
break;
74+
}
75+
76+
}
77+
78+
}
79+
}

0 commit comments

Comments
 (0)