-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice26.java
More file actions
72 lines (61 loc) · 2.54 KB
/
practice26.java
File metadata and controls
72 lines (61 loc) · 2.54 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
/*
Rock Paper Scissor Game
*/
import java.util.Scanner;
import java.util.Random;
public class practice26 {
public static void main(String[] args) {
String Player_turn; //User's input
String Computer_turn=""; //Computer's input
int Computer_no;
String response;
Scanner res = new Scanner(System.in);
Random num = new Random();
System.out.println("Hey, Human let's play Rock Paper Scissor");
System.out.println("Enter your choice\n" + "Rock='R' , Paper='P' , Scissor='S' ");
Computer_no = num.nextInt(3)+1; //Generate computer input
/*
Translating computer input using condition statements
*/
if(Computer_no==1){
Computer_turn="R";
}
else if(Computer_no==2){
Computer_turn="P";
}
else if(Computer_no==3){
Computer_turn="S";
}
/*
Getting player input which is stored in string
*/
System.out.println("Enter your turn: ");
Player_turn = res.next();
Player_turn = Player_turn.toUpperCase(); //Converting players turn to uppercase
System.out.println("Computer turn: "+Computer_turn);
//Use nested loops to see who wins
//If both choose same
if (Player_turn.equals(Computer_turn))
System.out.println("It is a tie!");
//If player choose rocks
else if (Player_turn.equals("R"))
if (Computer_turn.equals("S"))
System.out.println("Rock crushes Scissors! Human win!!");
else if (Computer_turn.equals("P"))
System.out.println("Paper catches Rock! Computer win!!");
//If player choose papers
else if (Player_turn.equals("P"))
if (Computer_turn.equals("S"))
System.out.println("Scissor cuts Paper in two halves! Computer win!!");
else if (Computer_turn.equals("R"))
System.out.println("Paper catches Rock. Human win!!");
//If player choose scissors
else if (Player_turn.equals("S"))
if (Computer_turn.equals("P"))
System.out.println("Scissor cuts Paper in two halves. Human win!!");
else if (Computer_turn.equals("R"))
System.out.println("Rock crushes Scissor! Computer win!!");
else
System.out.println("Invalid user input.");
}
}