2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Javaでじゃんけん

Last updated at Posted at 2020-09-08

openjdk インストール

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 = "グー";
				break;

			case 1 :
				strA = "チョキ";
				break;

			case 2 :
				strA = "パー";
				break;

			default:
				strA = "";
				break;
		}
		switch(comB){
			case 0 :
				strB = "グー";
				break;
			case 1 :
				strB = "チョキ";
				break;
			case 2 :
				strB = "パー";
				break;

			default:
				strB = "";
				break;
		}
		if(comA == comB){
			System.out.println(
					"コンピュータA:" + strA
					);
			System.out.println(
					"コンピュータB:" + strB
					);
			System.out.println(
					"あいこ"
					);
		}
		else if(((comA == 0) && (comB == 1)) || ((comA == 1) && (comB == 2)) || ((comA == 2) && (comB == 0))){
			System.out.println(
					"コンピュータA:" + strA
					);
			System.out.println(
					"コンピュータB:" + strB
					);
			System.out.println(
					"コンピュータAの勝ち"
					);
		}
		else{
			System.out.println(
					"コンピュータA:" + strA
					);
			System.out.println(
					"コンピュータB:" + strB
					);
			System.out.println(
					"コンピュータAの負け"
					);
		}
	}
}
# コンパイル
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
コンピュータA:グー
コンピュータB:チョキ
コンピュータAの勝ち

$ java Janken.java 1
コンピュータA:グー
コンピュータB:チョキ
コンピュータAの勝ち

$ java Janken.java 2
コンピュータA:チョキ
コンピュータB:グー
コンピュータAの負け
2
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?