Java猜拳,人机互动猜拳(机器随机生成剪刀石头布,判断谁赢)----三局两胜

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Random random = new Random();

        //计数器
        int count=0,countHuman=0,countRobot=0;
        //用户连续玩三局
        while (count<3){
            System.out.println("0:布");
            System.out.println("1:剪刀");
            System.out.println("2:石头");
            System.out.println("请选择要出的选项:");

            int human = sc.nextInt();
            int robot = random.nextInt(3);

            switch (human) {
                case 0: System.out.println("用户出的是:布");break;
                case 1: System.out.println("用户出的是:剪刀");break;
                case 2: System.out.println("用户出的是:石头");break;
            }
            switch (robot) {
                case 0: System.out.println("机器人出的是:布");break;
                case 1: System.out.println("机器人出的是:剪刀");break;
                case 2: System.out.println("机器人出的是:石头");break;
            }

            if(human == robot){
                System.out.println("平局");
            }else if(human==0 && robot==2){
                System.out.println("用户赢");
                countHuman++;
            }else if(human==2 && robot==0){
                System.out.println("机器赢");
                countRobot++;
            }else if (human > robot){
                System.out.println("用户赢");
                countHuman++;
            }else {
                System.out.println("机器赢");
                countRobot++;
            }
            System.out.println("----------------------------");
            count++;
        }

        System.out.println("用户赢"+countHuman+"次");
        System.out.println("机器赢"+countRobot+"次");

        if(countHuman==countRobot){
            System.out.println("平局");
        }else if (countHuman > countRobot){
            System.out.println("最终用户赢");
        }else {
            System.out.println("最终机器赢");
        }

    }

d3556c8c6b854ab89500fa354091e53d.png

优化

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Random random = new Random();
        //计数器
        int count = 0, countPilay = 0, countComputer = 0;

        while (true) {
            count++;
            //选择出拳
            System.out.println("请猜拳:0布,1剪刀,2石头");
            int play = sc.nextInt();
            int comptuer = random.nextInt(3);
            //展示出拳
            switch (play) {
                case 0:
                    System.out.println("用户出的是:布");
                    break;
                case 1:
                    System.out.println("用户出的是:剪刀");
                    break;
                case 2:
                    System.out.println("用户出的是:石头");
                    break;
            }
            switch (comptuer) {
                case 0:
                    System.out.println("机器人出的是:布");
                    break;
                case 1:
                    System.out.println("机器人出的是:剪刀");
                    break;
                case 2:
                    System.out.println("机器人出的是:石头");
                    break;
            }
            //比赢
            if (play == comptuer) {
                System.out.println("该局为平局");
            } else if (play == 0 && comptuer == 2) {
                System.out.println("该局用户赢");
                countPilay++;
            } else if (play == 2 && comptuer == 0) {
                System.out.println("该局机器赢");
                countComputer++;
            } else if (play > comptuer) {
                System.out.println("该局用户赢");
                countPilay++;
            } else {
                System.out.println("该局机器赢");
                countComputer++;
            }
            //三局两胜

            if(count == 3){
                if(countPilay==countComputer){
                    System.out.println("游戏结束,最终为平局");

                }else if (countPilay>countComputer){
                    System.out.println("游戏结束,最终用户获胜");
                }else {
                    System.out.println("游戏结束,最终电脑获胜");
                }
                //是否进行下一轮游戏
                System.out.println("-------是否还进行游戏(Y/N)-------");
                String inputs = sc.next();
                if (inputs.equals("Y")) {
                    count=0;
                    countPilay = 0;
                    countComputer = 0;
                    continue;
                } else if (inputs.equals("N")) {
                    break;
                } else {
                    System.out.println("输入有误,退出程序!");
                    break;
                }
            }else if (countPilay == 2 ) {
                System.out.println("游戏结束,最终用户获胜");
                //是否进行下一轮游戏
                System.out.println("-------是否还进行游戏(Y/N)-------");
                String inputs = sc.next();
                if (inputs.equals("Y")) {
                    count=0;
                    countPilay = 0;
                    countComputer = 0;
                    continue;
                } else if (inputs.equals("N")) {
                    break;
                } else {
                    System.out.println("输入有误,退出程序!");
                    break;
                }
            } else if (countComputer == 2) {
                System.out.println("游戏结束,最终电脑获胜");
                //是否进行下一轮游戏
                System.out.println("-------是否还进行游戏(Y/N)-------");
                String inputs = sc.next();
                if (inputs.equals("Y")) {
                    count=0;
                    countPilay = 0;
                    countComputer = 0;
                    continue;
                } else if (inputs.equals("N")) {
                    break;
                } else {
                    System.out.println("输入有误,退出程序!");
                    break;
                }
            }
            System.out.println("————————下一局————————");
        }

    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MaGgIeOo0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值