【发布时间】:2019-12-04 20:02:13
【问题描述】:
我正在写一个愚蠢的代码作为一个笑话,有点像猜数字。我认为这很好,直到我意识到只有当我按特定顺序输入数字时才会打印正确的东西。我有点初学者,所以我不确定为什么只有按顺序输入数字才能正确打印。这是一般的while循环的条件吗?有没有办法解决这个问题,这样数字的顺序就无关紧要了?任何见解将不胜感激。
这是我的代码:
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
int number;
int main()
{
printf("Enter a number!\n");
scanf("%d", &number);
while ((number != 69) && (number != 420))
{
printf("hmmm, not the number i was looking for... Enter another number!\n");
scanf("%d", &number);
while (number == 666)
{
printf("what are you, emo? try again!\n");
scanf("%d", &number);
while (number == 420)
{
printf("lol close, try the other Funny Number\n");
scanf("%d", &number);
while ((number != 69) && (number != 420))
{
printf("hmmm, not the number i was looking for... Enter another number!\n");
scanf("%d", &number);
}
while (number == 69)
{
printf("haha nice\n");
return 0;
}
}
}
}
}
【问题讨论】:
-
“猜数字”并没有准确地告诉我们程序应该做什么。细节在编程中很重要。我们需要准确的描述。请提供准确的输入、预期输出和实际输出。
-
看起来您在许多应该使用
if的地方使用while。对您的最佳建议是在调试器中运行您的程序并逐步执行以更好地了解代码在做什么。并使用一致的缩进更好地格式化您的代码 - 这实际上非常重要,不仅对于代码可读性而且能够检查您的逻辑结构是否正确。 -
如果您对程序应用一致的缩进,您可能会对其内部工作有一个全新的认识。
标签: c loops while-loop