File tree 1 file changed +45
-0
lines changed
1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
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 ) **
You can’t perform that action at this time.
0 commit comments