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

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


欢迎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);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用OpenCV-Python库识别石头剪刀布手势的示例代码: ```python import cv2 import numpy as np import math # 定义区域的颜色上下限 lower = np.array([0, 20, 70], dtype=np.uint8) upper = np.array([20, 255, 255], dtype=np.uint8) # 开启摄像头 cap = cv2.VideoCapture(0) while True: # 读取一帧 ret, frame = cap.read() if not ret: break # 镜像翻转 frame = cv2.flip(frame, 1) # 转换到HSV空间 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # 创建掩膜 mask = cv2.inRange(hsv, lower, upper) # 进行图像形态学操作 kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)) mask = cv2.erode(mask, kernel, iterations=2) mask = cv2.dilate(mask, kernel, iterations=2) # 找到轮廓 contours, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 如果找到了轮廓 if len(contours) > 0: # 找到最大的轮廓 contour = max(contours, key=cv2.contourArea) # 计算轮廓的外接圆 ((x, y), radius) = cv2.minEnclosingCircle(contour) # 计算轮廓的重心 M = cv2.moments(contour) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # 绘制重心和外接圆 cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2) cv2.circle(frame, center, 5, (0, 0, 255), -1) # 如果半径大于10 if radius > 10: # 计算手势方向 rect = cv2.minAreaRect(contour) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(frame, [box], 0, (0, 255, 0), 2) x1, y1 = box[0] x2, y2 = box[1] angle = math.atan2(y2 - y1, x2 - x1) * 180.0 / math.pi if angle < 0: angle += 180 if angle <= 60: gesture = "scissors" elif angle <= 120: gesture = "rock" else: gesture = "paper" # 在屏幕上显示手势 cv2.putText(frame, gesture, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2) # 显示结果 cv2.imshow("Frame", frame) cv2.imshow("Mask", mask) # 按下q键退出循环 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头 cap.release() # 关闭所有窗口 cv2.destroyAllWindows() ``` 代码中使用红色的石头剪刀布手势进行演示,可以根据实际情况调整颜色区间。使用此代码时需要安装OpenCV-Python库,可以通过以下命令进行安装: ``` pip install opencv-python ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值