循环练习题2-综合练习石头剪刀布

这篇博客介绍了一个简单的猜拳游戏,玩家可以与电脑AI进行对战。玩家输入数字选择石头、剪刀或布,电脑随机生成选择。游戏会根据输入判断胜负,并在结束时展示玩家的胜率等统计信息。代码中包含了游戏逻辑和用户交互的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码未精简,只做了简单验证,如有错误,还请指正。


欢迎XXX进入猜拳游戏
1.石头 2.剪刀 3.布 0.退出


请输入数字:
/* 此时等待用户输入数字,每个数字代表不同的效果,整个程序只有当用户输入0时才可以退出 否则不允许停止程 序,当用户输入> 1-3之间某个数字的时候,需要和电脑系统AI进行猜拳对比,并对用户输出反馈效果 查看如果做 1~3之间数据获取操作*/


欢迎XXX进入猜拳游戏
1.石头 2.剪刀 3.布 0.退出


请输入数字:
1
如果赢了 提示 恭喜你赢了
如果输了 提示 你输了
如果平局 提示 平局再来
//输入提示语句之后 需要再次显示 以下界面


欢迎XXX进入猜拳游戏
1.石头 2.剪刀 3.布 0.退出


请输入数字:
/*
当用户输入0是,猜拳游戏结束,输出以下界面,需要打印谁玩的,玩了多少局,赢了多少场,胜率是多少
*/
排行榜
****************************************************************** 姓名 总局数 赢场 胜率
XXX  0    0  xx%

import java.util.Scanner;

public class TwentySix {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean choice1 = true;
        int playtimes = 0;
        int winNumber = 0;
        int rival = 0;

        do {
            System.out.println("************************************************************");
            System.out.println("欢迎ThreeGoldXD进入猜拳游戏");
            System.out.println("1.石头     2.剪刀     3.布     0.退出");
            System.out.println("************************************************************");
            System.out.println("请输入数字:");
            int choice = input.nextInt();

            if (choice == 0) {
                choice1 = false;
                double winRate = (double) ((double) winNumber / (double) playtimes);
                double winRate1 = (double) Math.round(winRate * 100) / 100; //保留两位小数

                System.out.println("                  排行榜                              ");
                System.out.println("******************************************************************");
                System.out.println("姓名" + "\t\t" + "总局数" + "\t" + "胜场" + "\t" + "胜率");
                System.out.println("ThreeGoldXD" + "\t  " + playtimes + "\t\t " + winNumber + "\t\t" + (int) (winRate1 * 100) + "%");

            } else if (choice == 1 || choice == 2 || choice == 3) {
                playtimes++;
                rival = (int) (Math.random() * 3 + 1);

                System.out.println("-------------------");
                if (choice == 1) System.out.print("|您:✊  VS");
                else if (choice == 2) System.out.print("|您:✌  VS");
                else if (choice == 3) System.out.print("|您:🖐  VS");

                if (rival == 1) System.out.println("  电脑:✊|");
                else if (rival == 2) System.out.println("  电脑:✌|");
                else if (rival == 3) System.out.println("  电脑:🖐|");

                if (choice == rival) System.out.println("-----平局再来------");
                else {
                    choice++;
                    if (choice > 3) choice = 1;
                    if (choice == rival) {
                        System.out.println("----恭喜您赢了-----");
                        winNumber++;
                    } else System.out.println("------您输了-------");
                }


            } else System.out.println("输入错误,请重新输入");
            System.out.println();
            System.out.println();
        } while (choice1);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值