用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のかちー");
        }
    }
}
广告
将在 10 秒后关闭
bannerAds