鱼C论坛

 找回密码
 立即注册
查看: 1572|回复: 7

[已解决]这个随机数求余是怎么生成的啊 怎么感觉不对劲

[复制链接]
发表于 2019-10-13 21:00:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
就是我写了一个猜拳游戏  0 代表 拳头   1 代表 剪刀  2  代表  布
代码如下
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
int main()
{
        int player, computer, n, result, win=0, lose=0, draw=0, T;
        n = 1;
        system("cls");
        printf("let's play the finger-guessing game!\n");
        srand(time(0));
        while (n)
        {
                printf("input 0 mesns fist, 1 means shears and 2 means papper.\n");
                scanf_s("%d", &player);
                fflush(stdin);
                computer = rand() % 3;
                T = rand();
                printf("the rand is %d and the computer is %d", T, computer);
                system("pause");
                result = player - computer;
                if (result == 0)
                {
                        printf("平局!\n");
                        Sleep(1000);
                        system("cls");
                        draw++;
                }
                else if (result == 2)
                {
                        printf("winner is player!\n");
                        Sleep(1000);
                        system("cls");
                        win++;
                }
                else if (result == 1)
                {
                        printf("输了!\n");
                        Sleep(1000);
                        system("cls");
                        lose++;
                }
                else if (result == -1)
                {
                        printf("输了!\n");
                        Sleep(1000);
                        system("cls");
                        lose++;
                }
                else if (result == -2)
                {
                        printf("winner is player!\n");
                        Sleep(1000);
                        system("cls");
                        win++;
                }
                else
                {
                        printf("illegal argument!!!\n");
                        Sleep(1000);
                        system("cls");
                }
                printf("input 0 to leave and others play again\n");
                scanf_s("%d", &n);
                fflush(stdin);
        }
        printf("the result is The Player win %d times!\nThe Computer wins %d times!\nAnd ends in the draw %d times\n",win, lose, draw);
        system("pause");
        return 0;
}

统计结果是这样的
        0   1   2
  0    平  赢  输
        3   8    9
  1    输  平  赢
        13  10  0
        27  12  0
  2    赢  输  平
        3    6   9
输入1的时候一次没赢过....
最佳答案
2019-10-13 22:39:53
本帖最后由 superbe 于 2019-10-13 22:51 编辑

代码中 result == -1 时应该是赢了,result == -2 时应该是输了。把 -1 和 -2 交换一下。
随机数对3求余结果是0,1,2,没问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-10-13 21:01:47 | 显示全部楼层
纵列的0 1 2 是我自己输入的  横行的  0  1  2  是电脑求余的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-13 22:39:53 | 显示全部楼层    本楼为最佳答案   
本帖最后由 superbe 于 2019-10-13 22:51 编辑

代码中 result == -1 时应该是赢了,result == -2 时应该是输了。把 -1 和 -2 交换一下。
随机数对3求余结果是0,1,2,没问题。

评分

参与人数 1荣誉 +3 鱼币 +3 贡献 +2 收起 理由
fury可 + 3 + 3 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-13 23:21:31 | 显示全部楼层
superbe 发表于 2019-10-13 22:39
代码中 result == -1 时应该是赢了,result == -2 时应该是输了。把 -1 和 -2 交换一下。
随机数对3求余结 ...

谢谢指导....  这看了好久都没看出来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-13 23:24:52 | 显示全部楼层
superbe 发表于 2019-10-13 22:39
代码中 result == -1 时应该是赢了,result == -2 时应该是输了。把 -1 和 -2 交换一下。
随机数对3求余结 ...

怎么把求助帖取消来着?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-13 23:25:35 | 显示全部楼层
本帖最后由 jackz007 于 2019-10-13 23:39 编辑

     楼主判断输赢的逻辑有问题,我修改了一下:
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. int main(void)
  6. {
  7.         int player, computer, n, result, win=0, lose=0, draw=0, T                  ;
  8.         n = 1                                                                      ;
  9.         system("cls")                                                              ;
  10.         printf("let's play the finger-guessing game!\n")                           ;
  11.         while (n) {
  12.                 srand((unsigned int)time(NULL))                                    ;
  13.                 computer = rand() % 3                                              ;
  14.                 printf("input 0 mesns fist, 1 means shears and 2 means papper.\n") ;
  15.                 scanf("%d" , & player)                                             ;
  16.                 fflush(stdin)                                                      ;
  17.                 player = player % 3                                                ;
  18.                 result = player - computer                                         ;
  19.                 if (! result) {
  20.                         printf("平局!\n")                                          ;
  21.                         draw ++                                                    ;
  22.                 } else {
  23.                         if(result == -1 || result == 2) {
  24.                                 printf("winner is player!\n")                      ;
  25.                                 win ++                                             ;
  26.                         } else {
  27.                                 printf("loser is player!\n")                       ;
  28.                                 lose ++                                            ;
  29.                         }
  30.                 }
  31.                 Sleep(1000)                                                        ;
  32.                 printf("input 0 to leave and others play again\n")                 ;
  33.                 scanf("%d", & n)                                                   ;
  34.                 fflush(stdin)                                                      ;
  35.         }
  36.         printf("the result is The Player win %d times!\nThe Computer wins %d times!\nAnd ends in the draw %d times\n" , win , lose , draw) ;
  37.         system("pause")                                                                                                                    ;
  38. }
复制代码

评分

参与人数 1荣誉 +2 鱼币 +2 贡献 +1 收起 理由
fury可 + 2 + 2 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-13 23:32:19 | 显示全部楼层
jackz007 发表于 2019-10-13 23:25
楼主判断输赢的逻辑有问题,我修改了一下:

em。。。。是你这个简单多
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-13 23:55:36 | 显示全部楼层
fury可 发表于 2019-10-13 23:21
谢谢指导....  这看了好久都没看出来

看自己代码有时就这样,今天写代码就因为#define后面多了一个分号编译不过,找了半天才发现问题,可是像这种错误,看别人代码一眼就看出来了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-19 01:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表