apt install -y openjdk-14-jdk
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:
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(
"Losing computer A"
);
}
}
}
#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
Losing computer A
Recommended Posts