-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockPaperScissors.java
More file actions
95 lines (91 loc) · 3.22 KB
/
RockPaperScissors.java
File metadata and controls
95 lines (91 loc) · 3.22 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package loop;
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
String[] choices = {"Rock","Paper","Scissors"};
Scanner sc = new Scanner(System.in);
System.out.println("This is Rock Paper Scissors match");
System.out.print("How many rounds do you need :");
int rounds=sc.nextInt();
Random random = new Random();
String choice;
String randomChoice;
int randomIndex;
int userScore=0;
int compScore=0;
for (int i=1;i<=rounds;i++){
String winner;
System.out.println("Select your choice - Rock Paper or Scissor?");
choice=sc.next();
randomIndex=random.nextInt(choices.length);
randomChoice=choices[randomIndex];
winner=roundResults(choice,randomChoice);
if (winner.equalsIgnoreCase("User")){
userScore++;
System.out.println("You selected "+choice+" I selected "+randomChoice);
System.out.println("You win this round");
}else if (winner.equalsIgnoreCase("Comp")){
compScore++;
System.out.println("You selected "+choice+" I selected "+randomChoice);
System.out.println("I win this round");
}else{
System.out.println("You selected "+choice+" I selected "+randomChoice);
System.out.println("Its a draw");
}
}
if (userScore>compScore){
System.out.println("You win the competition!!!!");
System.out.println("Score : You - "+userScore+" Computer - "+compScore);
}else if (userScore<compScore){
System.out.println("You lose!!!");
System.out.println("Score : You - "+userScore+" Computer - "+compScore);
}else{
System.out.println("It is a draw");
System.out.println("Score : You - "+userScore+" Computer - "+compScore);
}
}
public static String roundResults(String user,String random){
String winner="";
if(user.equalsIgnoreCase("Rock")){
switch(random){
case "Rock":
winner="Draw";
break;
case "Paper":
winner="Comp";
break;
case "Scissor":
winner="User";
break;
}
}
if(user.equalsIgnoreCase("Paper")){
switch(random){
case "Rock":
winner="User";
break;
case "Paper":
winner="Draw";
break;
case "Scissor":
winner="Comp";
break;
}
}
if(user.equalsIgnoreCase("Scissor")){
switch(random){
case "Rock":
winner="Comp";
break;
case "Paper":
winner="User";
break;
case "Scissor":
winner="Draw";
break;
}
}
return winner;
}
}