我的代码错了吗?
Is my code wrong?
我是编码方面的新手,正在自学 C。
我正在做我的第一个练习,我要创建一个 "game"("More or Less") 其概念是:
-计算机在 1 和 100 之间选择一个随机数。
-我们得猜!
-找到神秘号码后游戏结束
我放置了函数循环 (do..while) 和函数 (if...else) 来让游戏继续进行,即使你没有找到神秘数字(除非你找到了!)
几天来我一直坚持我的代码原因,当我调试时,什么也没有发生(所以这是一个很好的消息)但是当我运行 也 什么都没有发生
我的代码是:
int main( int argc, char*argv[])
{
int numberYouChoose = 0;
int MysteryNumber = 0;
const int MAX = 100, MIN = 1;
printf("What's the number?\n");
scanf("%d", &numberYouChoose);
srand(time(NULL));
MysteryNumber = (rand() % (MAX - MIN + 1)) + MIN;
do{
printf("Boooooh Try again!");
}while(numberYouChoose != MysteryNumber);
if (numberYouChoose == MysteryNumber);
printf("Yay you found it!\n");
return 0;
}
像计算机一样思考,一步一步来...你问一次数字就再也不会问了,所以它会永远卡在你的工作中。每次用户失败时,您都需要询问。将您的 Do-while
更改为简单的 while
while (numberYouChoose != MysteryNumber) {
printf("Boooooh Try again!\n");
printf("What's the number?\n");
scanf_s("%d", &numberYouChoose);
}
printf("Yay you found it!\n");
编辑:
if (numberYouChoose == MysteryNumber);
{
printf("Yay you found it!\n");
}
这是多余的,当用户输入正确的数字时,您将退出 while
。
这就是完整的代码:
int main(int argc, char*argv[])
{
int numberYouChoose = 0;
int MysteryNumber = 0;
const int MAX = 100, MIN = 1;
printf("What's the number?\n");
scanf("%d", &numberYouChoose);
srand(time(NULL));
MysteryNumber = (rand() % (MAX - MIN + 1)) + MIN;
while (numberYouChoose != MysteryNumber)
{
printf("Boooooh Try again!\n");
printf("What's the number?\n");
scanf_s("%d", &numberYouChoose);
}
printf("Yay you found it!\n");
return 0;
}
您的代码存在一些问题:
- 正如胡安在他之前的回答中指出的那样,控制结构是错误的。简单地将 do-while 更改为 while-do 会使您的代码报告 'Boooh...' 在用户甚至有机会猜测之前(除非您重复询问一个数字,这不是一个好主意。
- 在您的代码行
if (numberYouChoose == MysteryNumber);
中,结尾的 ;
是一个空语句,因此将始终执行后面的 printf
行。
以下作品:
int main( int argc, char*argv[])
{
int numberYouChoose = 0;
int MysteryNumber = 0;
const int MAX = 100, MIN = 1;
srand(time(NULL));
MysteryNumber = (rand() % (MAX - MIN + 1)) + MIN;
while ( 1 )
{
printf("What's the number?\n");
scanf("%d", &numberYouChoose);
if (numberYouChoose == MysteryNumber)
{
printf("Yay you found it!\n");
break;
}
printf("Boooooh Try again!");
}
return 0;
}
( 1 )
的计算结果总是为真,因此,理论上这将永远循环下去。但是,当您猜对时,代码会报告这一点,然后 break
会导致 while 循环终止并且代码在 while 循环之后继续。
我是编码方面的新手,正在自学 C。
我正在做我的第一个练习,我要创建一个 "game"("More or Less") 其概念是:
-计算机在 1 和 100 之间选择一个随机数。
-我们得猜!
-找到神秘号码后游戏结束
我放置了函数循环 (do..while) 和函数 (if...else) 来让游戏继续进行,即使你没有找到神秘数字(除非你找到了!)
几天来我一直坚持我的代码原因,当我调试时,什么也没有发生(所以这是一个很好的消息)但是当我运行 也 什么都没有发生
我的代码是:
int main( int argc, char*argv[])
{
int numberYouChoose = 0;
int MysteryNumber = 0;
const int MAX = 100, MIN = 1;
printf("What's the number?\n");
scanf("%d", &numberYouChoose);
srand(time(NULL));
MysteryNumber = (rand() % (MAX - MIN + 1)) + MIN;
do{
printf("Boooooh Try again!");
}while(numberYouChoose != MysteryNumber);
if (numberYouChoose == MysteryNumber);
printf("Yay you found it!\n");
return 0;
}
像计算机一样思考,一步一步来...你问一次数字就再也不会问了,所以它会永远卡在你的工作中。每次用户失败时,您都需要询问。将您的 Do-while
更改为简单的 while
while (numberYouChoose != MysteryNumber) {
printf("Boooooh Try again!\n");
printf("What's the number?\n");
scanf_s("%d", &numberYouChoose);
}
printf("Yay you found it!\n");
编辑:
if (numberYouChoose == MysteryNumber);
{
printf("Yay you found it!\n");
}
这是多余的,当用户输入正确的数字时,您将退出 while
。
这就是完整的代码:
int main(int argc, char*argv[])
{
int numberYouChoose = 0;
int MysteryNumber = 0;
const int MAX = 100, MIN = 1;
printf("What's the number?\n");
scanf("%d", &numberYouChoose);
srand(time(NULL));
MysteryNumber = (rand() % (MAX - MIN + 1)) + MIN;
while (numberYouChoose != MysteryNumber)
{
printf("Boooooh Try again!\n");
printf("What's the number?\n");
scanf_s("%d", &numberYouChoose);
}
printf("Yay you found it!\n");
return 0;
}
您的代码存在一些问题:
- 正如胡安在他之前的回答中指出的那样,控制结构是错误的。简单地将 do-while 更改为 while-do 会使您的代码报告 'Boooh...' 在用户甚至有机会猜测之前(除非您重复询问一个数字,这不是一个好主意。
- 在您的代码行
if (numberYouChoose == MysteryNumber);
中,结尾的;
是一个空语句,因此将始终执行后面的printf
行。
以下作品:
int main( int argc, char*argv[])
{
int numberYouChoose = 0;
int MysteryNumber = 0;
const int MAX = 100, MIN = 1;
srand(time(NULL));
MysteryNumber = (rand() % (MAX - MIN + 1)) + MIN;
while ( 1 )
{
printf("What's the number?\n");
scanf("%d", &numberYouChoose);
if (numberYouChoose == MysteryNumber)
{
printf("Yay you found it!\n");
break;
}
printf("Boooooh Try again!");
}
return 0;
}
( 1 )
的计算结果总是为真,因此,理论上这将永远循环下去。但是,当您猜对时,代码会报告这一点,然后 break
会导致 while 循环终止并且代码在 while 循环之后继续。