为什么即使值为 true,我的 while 循环仍继续执行?
Why does my while loop keep executing even when the value is true?
我最初尝试使用 Do While 循环,但得到的结果与 while 循环相同。用户必须输入 28 - 31 之间的数字。第一次尝试时,如果用户输入的值正确,它将继续执行代码的下一部分。但是,如果用户输入了错误的值,它会再次要求输入一个数字,但无论他们输入什么,它都会不断重复。
#include <cs50.h>
#include <stdio.h>
int main(void)
{
printf("Days in month: ");
int daysInMonth = GetInt();
while (daysInMonth < 28 || daysInMonth > 31)
{
printf("Days in month: ");
int daysInMonth = GetInt();
printf("%i\n", daysInMonth);
}
printf("Pennies on the first day: ");
int pennies = GetInt();
while (pennies < 1)
{
printf("Pennies on the first day: ");
int pennies = GetInt();
}
}
printf 语句用于调试目的,以测试 daysInMonth
是否正在重新分配值。
TL:DR 您的代码中有 两个 个不同的变量,名为 daysInMonth
,重叠 scope,这会产生问题。
你对 pennies
变量也有同样的问题。
详细说明,
int daysInMonth = GetInt();
创建一个 (another) 变量,它是 while
循环体的局部变量。它隐藏了外部 daysInMonth
变量,因此在条件检查中,值永远不会改变。改为
daysInMonth = GetInt();
在 while
循环体内。
引用 c11
,章节 §6.2.1,标识符的范围
[...] If the declarator or type specifier that
declares the identifier appears inside a block or within the list of parameter declarations in
a function definition, the identifier has block scope, which terminates at the end of the
associated block. [...]
和
[...] If an identifier designates two different entities in the same name
space, the scopes might overlap. If so, the scope of one entity (the inner scope) will end
strictly before the scope of the other entity (the outer scope). Within the inner scope, the
identifier designates the entity declared in the inner scope; the entity declared in the outer
scope is hidden (and not visible) within the inner scope.
在第一个 while
中,您有:
int daysInMonth = GetInt();
这定义了一个 new 变量 daysInMonth
,局部于 while
的 body,隐藏同名的外部变量。因此,在 while
循环的 condition 中,您使用外部 daysInMonth
,在主体中,您使用内部 daysInMonth
。
我猜您想从此行中删除 int
部分,只修改外部 daysInMonth
.
其实你在第二个while
循环中也有同样的情况。
我最初尝试使用 Do While 循环,但得到的结果与 while 循环相同。用户必须输入 28 - 31 之间的数字。第一次尝试时,如果用户输入的值正确,它将继续执行代码的下一部分。但是,如果用户输入了错误的值,它会再次要求输入一个数字,但无论他们输入什么,它都会不断重复。
#include <cs50.h>
#include <stdio.h>
int main(void)
{
printf("Days in month: ");
int daysInMonth = GetInt();
while (daysInMonth < 28 || daysInMonth > 31)
{
printf("Days in month: ");
int daysInMonth = GetInt();
printf("%i\n", daysInMonth);
}
printf("Pennies on the first day: ");
int pennies = GetInt();
while (pennies < 1)
{
printf("Pennies on the first day: ");
int pennies = GetInt();
}
}
printf 语句用于调试目的,以测试 daysInMonth
是否正在重新分配值。
TL:DR 您的代码中有 两个 个不同的变量,名为 daysInMonth
,重叠 scope,这会产生问题。
你对 pennies
变量也有同样的问题。
详细说明,
int daysInMonth = GetInt();
创建一个 (another) 变量,它是 while
循环体的局部变量。它隐藏了外部 daysInMonth
变量,因此在条件检查中,值永远不会改变。改为
daysInMonth = GetInt();
在 while
循环体内。
引用 c11
,章节 §6.2.1,标识符的范围
[...] If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the end of the associated block. [...]
和
[...] If an identifier designates two different entities in the same name space, the scopes might overlap. If so, the scope of one entity (the inner scope) will end strictly before the scope of the other entity (the outer scope). Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.
在第一个 while
中,您有:
int daysInMonth = GetInt();
这定义了一个 new 变量 daysInMonth
,局部于 while
的 body,隐藏同名的外部变量。因此,在 while
循环的 condition 中,您使用外部 daysInMonth
,在主体中,您使用内部 daysInMonth
。
我猜您想从此行中删除 int
部分,只修改外部 daysInMonth
.
其实你在第二个while
循环中也有同样的情况。