#include
#include
#include
#include
#include
int main(void)
{
unsigned int max = 3;
unsigned int guess = 0;
unsigned int chones = 0;
unsigned int limit = 20;
char answer = 'n';
printf("这是一个猜数字游戏!游戏开始时!\n");
srand(time(NULL));
while(true)
{
chones = 1 + rand() % limit;
printf("这是一个猜数字游戏(范围1~20)\n");
for( unsigned int count = max ; count > 0 ; count-- )
{
printf("你还有%u 次机会!\n", count);
scanf("%u",&guess);
if(guess == chones)
{
printf("恭喜你猜对了!\n");
break;
}
if(guess < 1||guess > 20)
printf("请输入正确范围(1~20)\n");
else
printf("对不起!你猜错了!\n");
}
printf("程序结束!你没有猜对!这个数是:%u 继续? (Y or N)\n",chones);
scanf(" %c", &answer);
if(tolower(answer) == 'n')
break;
}
printf("程序结束!\n");
return 0;
}
这是一个猜数字游戏,共有3次机会,3次猜不到提示幸运数字并询问是否继续
问题1: 假如第一次猜对了,执行了break; 那么程序跳转到哪?
问题2: 幸运数字是怎么随着所猜的次数变化而变化的?
问题3:这两行语句unsigned int max = 3;for( unsigned int count = max ; count > 0 ; count-- )改为unsigned int count = 0;for(unsigned int i = 1 ; i <= count ; i++)后
为什么三次猜不对继续猜的时候出现问题?
望大神指教!(纸上得来终觉浅,绝知此事要躬行)
一.题目:猜数字游戏
功能要求:计算机产生随机数,猜中即胜,猜不中,提示是大了,还是小了,继续猜,直到猜到,给出所用时间、次数和评语(要求评语多样性、滑稽)。
界面要求:简洁
二.设计概要
该程序是由: 1.生成随机数函数 ......
答案就在这里:猜数字游戏(c语言程序)
----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?
假如第一次猜对了,执行了break; break 语句的功能是跳出当前的循环,所以猜对后会直接退出程序。输出的提示:恭喜你猜对了! 估计是看不到的,因为没有 getch() 之为类的语句。
问题1:break跳出的当前循环体,也就是for循环,接着执行的是打印 printf("程序结束!你没有猜对!这个数是:%u 继续? (Y or N)\n",chones);
问题2:每一次的while循环产生一个随机幸运数字,三次猜测的是同一个幸运数字。
问题3:改为unsigned int count = 0;for(unsigned int i = 1 ; i <= count ; i++)后,count为0,for循环根本不执行。
问题1: 假如第一次猜对了,执行了break; 那么程序跳转到哪?
while(1)
{
//do something...
break; //此处跳出while循环
}
while(1)
{
for(int i = 1; i < 10; ++i)
{
//do something...
}
//do something...
break; //此处跳出while循环,因为for循环并没有把break包含在内
}
while(1)
{
for(int i = 1; i < 10; ++i)
{
//do something...
break; //跳出for循环
}
//do something...
break; //跳出while循环
}
//始终记住跳出的是包含它(break)最近的循环,还有可以搜下contiune的区别
问题2: 幸运数字是怎么随着所猜的次数变化而变化的?
你可以搜下srand(time(NULL));,使用和不使用的时候会对rand()造成什么效果
问题3:这两行语句unsigned int max = 3;for( unsigned int count = max ; count > 0 ; count-- )改为unsigned int count = 0;for(unsigned int i = 1 ; i <= count ; i++)后
为什么三次猜不对继续猜的时候出现问题?
for( unsigned int count = 0 ; count < max; count++ )
printf("你还有%u 次机会!\n", max - count);