C语言:怎样把这个简单小游戏的do while 循环改为for循环?

#include<stdio.h>#include<stdlib.h>#include<time.h>#include<conio.h>#defineMAX_NUMBER... #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define MAX_NUMBER 100
int secret_number;
void initialize_number_generator(void);
void choose_new_secret_number(void);
void read_guesses(void);
main()
{
char command,at;
printf("guess the secret number between 1 and %d.\n\n",MAX_NUMBER);
initialize_number_generator();
do{
choose_new_secret_number();
printf("a new number has been chosen.\n");
read_guesses();
printf("play again?(Y/N)");
scanf("%c",&command);

at= getchar();
printf("\n");}while(at=='y'||at=='Y');
getchar();
return 0;}
void initialize_number_generator(void)
{srand((unsigned) time(NULL));
}
void choose_new_secret_number(void)
{secret_number=rand()%MAX_NUMBER +1;
}
void read_guesses(void)
{int guess,num_guess=0;
for(;;){
num_guess++;
printf("enter guess:");
scanf("%d",&guess);
if(guess==secret_number){
printf("you won in %d guesses.\n\n\a\a",num_guess);
return;}
else if (guess<secret_number)
printf("too low,try again.\n");
else printf("too hign,try again.\n");
}
}
以上程序中,我想把“
do{
choose_new_secret_number();
printf("a new number has been chosen.\n");
read_guesses();
printf("play again?(Y/N)");
scanf("%c",&command);

at= getchar();
printf("\n");}while(at=='y'||at=='Y');
getchar();
return 0;}”用FOR循环来写,我怎样改都不行,求教!谢谢!
展开
 我来答
匿名用户
2009-02-21
展开全部
这么改:
do{
choose_new_secret_number();
printf("a new number has been chosen.\n");
read_guesses();
printf("play again?(Y/N)");
scanf("%c",&command);

at= getchar();
printf("拍卖羡\n");}while(at=='y'||at=='Y');

改为:
choose_new_secret_number();
printf("a new number has been chosen.\n");
read_guesses();
printf("play again?(Y/N)");
scanf("%c",&command);

at= getchar();
printf("\n");
for(;at=='y'||at=='Y';)
{
choose_new_secret_number();
printf("a new number has been chosen.\n");
read_guesses();
printf("play again?(Y/N)");
scanf("%c",&command);

at= getchar();
printf("\n");}

由于do/袭拍while循环的特点是又一次无条件执行(就是do的第一次执行),所以改成for语句后,需要先将配芦循环体提出来先执行一次
suiyue_2009
2009-02-21 · TA获得超过846个赞
知道小有建树答主
回答量:1248
采纳率:0%
帮助的人:876万
展开全部
for (int i=1;;i++)
{
choose_new_secret_number();
printf("a new number has been chosen.\兄耐n");
read_guesses();
printf("play again?(Y/N)");
scanf("%c",&command);

at= getchar();
printf("\羡敏春n"拿仔);
if (command != 'Y' || command != 'y')
{
break;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
asanasdake
2009-02-21 · TA获得超过743个赞
知道小有建树答主
回答量:410
采纳率:0%
帮助的人:0
展开全部
do {
A;
} while(B);
等价于
A;
for(;B;)
{
A;
}
都按照这卜埋团个模式套就型橘行了液并
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
seclery
2009-02-21 · TA获得超过198个赞
知道答主
回答量:104
采纳率:0%
帮助的人:0
展开全部
加个条件转移吧,用break;跳出试试
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式