这个随机数求余是怎么生成的啊 怎么感觉不对劲
就是我写了一个猜拳游戏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 输平赢
13100
27120
2 赢输平
3 6 9
输入1的时候一次没赢过.... 纵列的0 1 2 是我自己输入的横行的012是电脑求余的 本帖最后由 superbe 于 2019-10-13 22:51 编辑
代码中 result == -1 时应该是赢了,result == -2 时应该是输了。把 -1 和 -2 交换一下。
随机数对3求余结果是0,1,2,没问题。
superbe 发表于 2019-10-13 22:39
代码中 result == -1 时应该是赢了,result == -2 时应该是输了。把 -1 和 -2 交换一下。
随机数对3求余结 ...
谢谢指导....这看了好久都没看出来 superbe 发表于 2019-10-13 22:39
代码中 result == -1 时应该是赢了,result == -2 时应该是输了。把 -1 和 -2 交换一下。
随机数对3求余结 ...
怎么把求助帖取消来{:9_237:}着? 本帖最后由 jackz007 于 2019-10-13 23:39 编辑
楼主判断输赢的逻辑有问题,我修改了一下:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main(void)
{
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") ;
while (n) {
srand((unsigned int)time(NULL)) ;
computer = rand() % 3 ;
printf("input 0 mesns fist, 1 means shears and 2 means papper.\n") ;
scanf("%d" , & player) ;
fflush(stdin) ;
player = player % 3 ;
result = player - computer ;
if (! result) {
printf("平局!\n") ;
draw ++ ;
} else {
if(result == -1 || result == 2) {
printf("winner is player!\n") ;
win ++ ;
} else {
printf("loser is player!\n") ;
lose ++ ;
}
}
Sleep(1000) ;
printf("input 0 to leave and others play again\n") ;
scanf("%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") ;
} jackz007 发表于 2019-10-13 23:25
楼主判断输赢的逻辑有问题,我修改了一下:
em。。。。是你这个简单多 fury可 发表于 2019-10-13 23:21
谢谢指导....这看了好久都没看出来
看自己代码有时就这样,今天写代码就因为#define后面多了一个分号编译不过,找了半天才发现问题,可是像这种错误,看别人代码一眼就看出来了{:10_266:}
页:
[1]