为什么这个do while循环在c中不起作用



很抱歉这可能是一个常见的问题,但我的代码比我看到的任何其他问题都简单得多,而且仍然无法工作。

我的代码:

#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n;
do
{
n = get_int("Width: ");
} 
while (n < 1);
}

此代码是哈佛cs50课程的精确副本。我所期望的是,如果n小于1,它将再次提示用户,直到输入1或以上的值。然而,它只要求我宽度一次,即使我输入0并完成。

这是处理此类问题的方法:

您可以开始使用调试器,并在以下行放置断点:

int main(void)
{
int n;
do
{
n = get_int("Width: ");
} // here you set your breakpoint
while (n < 1);
}

您为变量n添加一个监视并检查该值。

另一种方法如下:

int main(void)
{
int n;
do
{
n = get_int("Width: ");
printf("The value of n is [%d]", n); // this line shows the value of n
} 
while (n < 1);
}

一句话:您需要调查n的值,或者使用调试器,或者在屏幕上显示它。

最新更新