麦子
小水手

UID 30865
精华
0
积分 2
帖子 2
阅读权限 10
注册 2007-12-15
状态 离线
|
请教一个程序的问题
/*这是一个C的猜数字的游戏,在1-100的数字中随机产生一个数,然后让玩家去猜。且最多只能猜10次。
*/
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
main()
{
int count,number,guess;
char choice;
clrscr();
printf("Welcome to play this number game!! you will have 10 trys.\n");
printf("Start?(Y/N)\n");
scanf("%c",&choice);
if(toupper(choice)=='N')
exit(0);
while(toupper(choice)=='Y')
{
count=0;
randomize();
number=random(100)+1;
printf("\nInput an integer number(1-100):");
scanf("%d",&guess);
while(!(guess==number))
{count++;
if(guess>number)
printf("\nYour answer is High,try again.");
if(guess<number)
printf("\nYour answer is Low,try again.");
if(count==5)
printf("\nBe careful! You have tried %d times!!",count);
if(count==10)
{
printf("\nYou have tried %d times.GAME OVER!",count);
printf("\nNext?(Y/N)");
scanf("%c",&choice);
if(toupper(choice)=='N')
exit(0);
if(toupper(choice)=='Y')
break;
}
printf("\nInput an integer number(1-100):");
scanf("%d",&guess);
}/*没有猜对时的情形*/
while(guess==number)
{
printf("\nCongratulations! You are so clever!");
printf("\n Beat youself! try again?(Y/N)");
scanf("%c",&choice);
if(toupper(choice)=='N')
exit(0);
if(toupper(choice)=='Y')
break;
}/*猜对时的情形*/
}
}
我不明白C中是否可以在WHILE中嵌套一个WHILE再嵌套IF语句,然后在IF语句中用SCANF语句。我做了个其他的例子,发现又可以。
可是,我在程序中当COUNT>10时,没有显示SCANF语句和接下来的判断直接又进入了下次的游戏。
请高手分析一下,谢谢了~
|
|