[Java] Rock Paper Scissors in Java

1 minute read

openjdk installation

apt install -y openjdk-14-jdk

Source

Janken.java


import java.util.Random;

public class Janken {
public static void main(String args[]) {

Random rand = new Random(Integer.valueOf(args[0]));
int comA = rand.nextInt(3);
int comB = rand.nextInt (3);
String strA, strB;
strA = "";
strB = "";
switch(comA){
case 0:
strA = "goo";
break;

case 1:
strA = "choki";
break;

case 2:
strA = "par";
break;

default: default:
strA = "";
break;
}
switch(comB){
case 0:
strB = "goo";
break;
case 1:
strB = "choki";
break;
case 2:
strB = "par";
break;

default:
strB = "";
break;
}
if(comA == comB){
System.out.println(
"Computer A:" + strA
);
System.out.println(
"Computer B:" + strB
);
System.out.println(
"Aiko"
);
}
else if(((comA == 0) && (comB == 1)) || ((comA == 1) && (comB == 2)) || ((comA == 2) && (comB == 0 ))) {
System.out.println (
"Computer A:" + strA
);
System.out.println(
"Computer B:" + strB
);
System.out.println(
"Computer A wins"
);
}
else{
System.out.println(
"Computer A:" + strA
);
System.out.println(
"Computer B:" + strB
);
System.out.println(
"Computer A loses"
);
}
}
}
# Compile
javac Janken.java
$ java Janken.java
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at Janken.main (Janken.java:7)

$ java Janken.java 0
Computer A: Goo
Computer B: Choki
Computer A wins

$ java Janken.java 1
Computer A: Goo
Computer B: Choki
Computer A wins

$ java Janken.java 2
Computer A: Choki
Computer B: Goo
Computer A loses

Tags:

Updated: