Skip to content

Commit d0158ac

Browse files
Exercise 2 Solution
1 parent 2de7475 commit d0158ac

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

41.Exercise_2_Solution/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Java Tutorial: Exercise 2 - Solution
2+
3+
### Code as described in the video:
4+
5+
```
6+
import java.util.Random;
7+
import java.util.Scanner;
8+
9+
public class cwh_41_ex2sol {
10+
public static void main(String[] args) {
11+
// 0 for Rock
12+
// 1 for Paper
13+
// 2 for Scissor
14+
15+
Scanner sc = new Scanner(System.in);
16+
System.out.print("Enter 0 for Rock, 1 for Paper, 2 for Scissor ");
17+
int userInput = sc.nextInt();
18+
19+
Random random = new Random();
20+
int computerInput = random.nextInt(3);
21+
22+
if (userInput == computerInput) {
23+
System.out.println("Draw");
24+
}
25+
else if (userInput == 0 && computerInput == 2 || userInput == 1 && computerInput == 0
26+
|| userInput == 2 && computerInput == 1) {
27+
System.out.println("You Win!");
28+
} else {
29+
System.out.println("Computer Win!");
30+
}
31+
// System.out.println("Computer choice: " + computerInput);
32+
if(computerInput==0){
33+
System.out.println("Computer choice: Rock");
34+
}
35+
else if(computerInput==1){
36+
System.out.println("Computer choice: Paper");
37+
}
38+
else if(computerInput==2){
39+
System.out.println("Computer choice: Scissors");
40+
}
41+
}
42+
}
43+
```
44+
45+
**Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-41/UltimateJavaCheatSheet.pdf)**

0 commit comments

Comments
 (0)