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循环来写,我怎样改都不行,求教!谢谢! 展开
#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循环来写,我怎样改都不行,求教!谢谢! 展开
4个回答
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语句后,需要先将配芦循环体提出来先执行一次
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语句后,需要先将配芦循环体提出来先执行一次
展开全部
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;
}
}
{
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;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
do {
A;
} while(B);
等价于
A;
for(;B;)
{
A;
}
都按照这卜埋团个模式套就型橘行了液并
A;
} while(B);
等价于
A;
for(;B;)
{
A;
}
都按照这卜埋团个模式套就型橘行了液并
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
加个条件转移吧,用break;跳出试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询