标题:猜数游戏
只看楼主
陈伟煌08
Rank: 1
等 级:新手上路
帖 子:4
专家分:5
注 册:2014-9-20
结帖率:50%
已结贴  问题点数:10 回复次数:2 
猜数游戏
#include<stdio.h>
#include<stdlib.h>
#include<time.h>/*头文件*/
void main()
{
    int ia,ib;
    long ic;
    char ca,cb,cc;
    time_t beg,end; /*定义时间变量*/
    printf("Do you want to play this game?('y' or 'n')\n");
    ca=getchar();
    while(ca!='n') /*循环*/
    {
        ia=rand(100); /*产生随机数*/
        printf("Now the system have produced a random number between 0 to 99,enter your guess:\n");
        scanf("%d",&ib);
        time(&beg); /*获取当前时间*/
        while(ib!=ia)
        {
            if(ib>ia)
            {
                printf("please input a smaller number!\n");
            }
            if(ib<ia)
            {
                printf("please input a bigger number!\n");
            }
            scanf("%d",&ib);
        }
        time(&end); /*获取结束时间*/
        ic=end-beg; /*计算所花时间*/
        printf("Success!It took you %ld seconds to finish\n",ic);
        {
            if(ic<15)
            {
                printf("you are clever!\n");
            }
            else if(ic<25)
            {
                printf("you are normal!\n");
            }
            else
            {
                printf("you are stupid!\n");
            }
        }
        getchar();
        printf("Do you want to play this game?('y' or 'n')\n");
        scanf("%c",&ca);
    }
}

这是个猜数的程序,可是为什么正确一次后想要再次游戏,不管输入什么数字,程序总是输出“please input a bigger number!”   ???
搜索更多相关主题的帖子: produced between include system number produced between include system number 
2014-10-19 13:22
wssy213
Rank: 12Rank: 12Rank: 12
来 自:湖南
等 级:贵宾
威 望:10
帖 子:967
专家分:3703
注 册:2014-6-6
得分:5 
ia=rand(100); /*产生随机数*/

rand()函数有参数?

这个参数对它是无效的,它产生的伪随机数是在0-RAND_MAX之间,所以才会出现你上面的情况

坚持----------------------------------唯一的道路
shit ! ! !
2014-10-19 15:20
erty1001
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:4
帖 子:331
专家分:1433
注 册:2014-8-31
得分:5 
简单说说:
楼上正解
正是问题所在,极有可能生成一个比较大的数字
应该R=rand()%100;
但是%运算又不是那么的平均分布,所以好的随机程序也要考验程序员的功底
2014-10-19 16:19



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-437423-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.208603 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved