首页 > 代码库 > 房上的猫:人机猜拳项目

房上的猫:人机猜拳项目

1.首先定义成员变量:

  

    int select1;// 人 选择
    int select2;// 角色 选择
    String choice1;// 人 选择结果
    String choice2;// 机 选择结果
    String choiceMan;// 角色 选择结果
    boolean a1;// 人 循环开关
    boolean a2;// 角色 循环开关
    int random = (int) (Math.random() * 3);// 机 随机数选择

2.定义起始页方法:

public void Copy() {// 起始页
        System.out.println("--------------------欢迎进入游戏世界------------------");
        System.out.println("\n\n");
        System.out.println("\t\t***************");
        System.out.println("\t\t**c猜拳,开始**");
        System.out.println("\t\t***************");
        System.out.println("\n\n");
        System.out.println("出拳规则:1.剪刀\t2.石头\t3.布");
    }

 

3.定义人的选择方法:

public void Man() {//
        Scanner input = new Scanner(System.in);
        do {
            System.out.print("\n请出拳:1.剪刀 2.石头 3.布(输入相应数字):");
            select1 = input.nextInt();
            a1 = false;
            switch (select1) {
            case 1:
                choice1 = "剪子";
                break;
            case 2:
                choice1 = "石头";
                break;
            case 3:
                choice1 = "布";
                break;
            default:
                System.out.println("请做出正确选择!");
                a1 = true;
                break;
            }
        } while (a1 == true);
    }

4.定义角色选择方法:

public void Choice() {// 角色
        Scanner input = new Scanner(System.in);
        do {
            System.out.print("请选择对方的角色(1.刘备 2.孙权 3.曹操):");
            select2 = input.nextInt();
            a2 = false;
            switch (select2) {
            case 1:
                choiceMan = "刘备";
                break;
            case 2:
                choiceMan = "孙权";
                break;
            case 3:
                choiceMan = "曹操";
                break;
            default:
                System.out.println("请做出正确选择!");
                a2 = true;
                break;
            }
        } while (a2 == true);
    }

5.定义随机数产生的出拳方法:

    public void Machine() {//
        if (random == 0.0) {
            random = 3;
        }
        switch (random) {
        case 1:
            choice2 = "剪子";
            break;
        case 2:
            choice2 = "石头";
            break;
        case 3:
            choice2 = "布";
            break;
        }
    }

6.最后使用main方法将各方法拼接起来:

public static void main(String[] args) {// 执行主页
        Second_Sheet bdqn = new Second_Sheet();
        Scanner input = new Scanner(System.in);
        String choice0 = null;
        int bout = 0;
        int bout1 = 0;
        int bout2 = 0;
        int bout3 = 0;
        bdqn.Copy();
        System.out.print("请输入你的姓名:");
        String name = input.next();
        bdqn.Choice();
        System.out.println(bdqn.choiceMan + " VS " + name + "\t对战\n");
        System.out.print("要开始吗?(开始按y)");
        do {
            choice0 = input.next();
            if (choice0.equals("y")) {
                bdqn.Man();
                System.out.println("你出拳:" + bdqn.choice1);
                bdqn.Machine();
                System.out.println(bdqn.choiceMan + "出拳:" + bdqn.choice2);
                System.out.println("结果说:");
                if (bdqn.select1 == bdqn.random) {
                    System.out.println("和局,真衰!嘿嘿,等着瞧吧!\n");
                    bout3++;
                } else if ((bdqn.select1 == 1 && bdqn.random == 3)
                        || (bdqn.select1 == 2 && bdqn.random == 1)
                        || (bdqn.select1 == 3 && bdqn.random == 2)) {
                    System.out.println("恭喜,你赢了!\n");
                    bout1++;
                } else {
                    System.out.println("^_^,你输了,真笨!\n");
                    bout2++;
                }
                System.out.print("是否开始下一轮?(开始按y):");
                bout++;
            } else {
                break;
            }
        } while (choice0.equals("y"));
        double result = (double) (bout1 / bout);
        System.out.println("-------------------------");
        System.out.println(bdqn.choiceMan + " VS " + name);
        System.out.println("对战次数:" + bout);
        System.out.print("结果:");
        System.out.println(name + "胜利:" + bout1);
        System.out.println(bdqn.choiceMan + "胜利:" + bout2);
        System.out.println("和局:" + bout3);
        if (result > 0.5) {
            System.out.println("恭喜恭喜!");
        } else if (result < 0.5) {
            System.out.println("呵呵,笨笨,下次加油啊!");
        } else {
            System.out.println("不错哦!");
        }
        System.out.println("-------------------------");
    }

 

房上的猫:人机猜拳项目