读书人

猜数字游戏总异常

发布时间: 2012-03-20 14:01:10 作者: rapoo

猜数字游戏总错误
猜数字游戏
提示数字为0-9只间的,输入字母和其它数就报错,要求重新输入:猜对时根据输入次数给予成绩A B C 3等级。
/*chen you cai*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
int x;/*随机数*/
int y;/*输入数*/
int cout=0;/*输入次数*/
void suijishu();/*随机数生成*/
void shurushu();/输入函数*/
void chuli();/*处理两数*/
void jieguo();/*结果显示*/
main()
{suijishu();
shurushu();
chuli();
jieguo();
}
void suijishu()
{srand((unsigned)time(NULL));
x=(unsigned)rand()%10;
}
void shurushu()
{printf( "input a 0-9 num:\n ");
scanf( "%s ",&y);
cout++;
while(isalpha(y))/*判断是否为字母,如果是就为真,执行以下语句*/
{printf( "the num is error!\n please input a 0-9 num again:\n ");
scanf( "%s ",&y);
cout++;
}
}

void chuli()
{if(x> y){printf( "the num is too small!\n ");
shurushu();
chuli();
};
if(x <y){printf( "the num is too large!\n ");
shurushu();
chuli();
};
if(x==y);
}

void jieguo()
{printf( "the num is %d,you are right!you guess %d times! ",x,cout);
if(cout <=3)printf( "you are A!\n ");
else if(cout <=7)printf( "you are B!\n ");
else printf( "you are C!\n ");
}
用TC2.0编译通过,可是当我输入数字5或6等时全说“the num is too large”。。我一直输到0了还是那样。。请问一下怎么回事。。我看不出了 请大家帮我下




[解决办法]
void shurushu()
{printf( "input a 0-9 num:\n ");
scanf( "%s ",&y);//这里错了,改为scanf( "%d ",&y);


cout++;
while(isalpha(y))/*判断是否为字母,如果是就为真,执行以下语句*/
{printf( "the num is error!\n please input a 0-9 num again:\n ");
scanf( "%s ",&y);//这里错了,改为scanf( "%d ",&y);
cout++;
}
}

由于输入的是0--9之间的数字,当作字符处理就成了0--9对应的ASCII码,一定大于0-9

读书人网 >C语言

热点推荐