4
9

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 5 years have passed since last update.

javaじゃんけん

Last updated at Posted at 2017-10-16

なんとなくじゃんけんぷろぐらむを作ってみました。

janken.java
import java.util.Random;

public class janken {
    // ぐーちょきぱー定数定義
    int GU = 0;
    int CHOKI = 1;
    int PA = 2;

    // プレイヤー;
    int player1;
    int player2;
    // 手
    String te1;
    String te2;

    // プレイヤー1の出す手を乱数で決める
    public void player1() {
        Random random = new Random();
        int hand1 = random.nextInt(3);
        switch (hand1) {
            case 0:
                player1 = GU;
                te1 = "ぐー";
                break;
            case 1:
                player1 = CHOKI;
                te1 = "ちょき";
                break;
            case 2:
                player1 = PA;
                te1 = "ぱー";
                break;
        }
    }
    // プレイヤー2の出す手を乱数で決める
    public void player2() {
        Random random = new Random();
        int hand2 = random.nextInt(3);
        switch (hand2) {
            case 0:
                player2 = GU;
                te2 = "ぐー";
                break;
            case 1:
                player2 = CHOKI;
                te2 = "ちょき";
                break;
            case 2:
                player2 = PA;
                te2 = "ぱー";
                break;
        }
    }

    public static void main(String[] args) {
        janken janken = new janken();
        janken.player1();
        janken.player2();


        System.out.println("player1とplayer2でじゃんけんをします。");
        System.out.println("player1の手: " + janken.te1);
        System.out.println("player2の手: " + janken.te2);

        // 判定
        if (janken.player1 == 0 && janken.player2 == 1) {
            System.out.println("ぷれいやー1のかちー");
        } else if (janken.player1 == 1 && janken.player2 == 2) {
            System.out.println("ぷれいやー1のかちー");
        } else if (janken.player1 == 2 && janken.player2 == 0) {
            System.out.println("ぷれいやー1のかちー");
        } else if (janken.player1 == janken.player2) {
            System.out.println("ひきわけ");
        } else {
            System.out.println("ぷれいやー2のかちー");
        }
    }
}
4
9
0

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
4
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?