Rock-paper-scissors in Java

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:
				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

Rock-paper-scissors in Java
Rock-paper-scissors app in Java
java rock-paper-scissors
Rock-paper-scissors game for beginners in Java
Partization in Java
Changes in Java 11
Pi in Java
FizzBuzz in Java
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
I made a rock-paper-scissors game in Java (CLI)
Try using RocksDB in Java
Read binary files in Java 1
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
[Neta] Sleep Sort in Java
Edit ini in Java: ini4j
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①
I made roulette in Java.
Implement two-step verification in Java
Refactoring: Make Blackjack in Java
Write flyway callbacks in Java
Topic Analysis (LDA) in Java
Importing Excel data in Java 2
NEologd preprocessing in Java neologdn-java