小编Too*_*his的帖子

为什么我的while循环即使在值为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();

     }

}
Run Code Online (Sandbox Code Playgroud)

printf语句用于调试目的,以测试是否daysInMonth重新分配值.

c scope loops while-loop cs50

2
推荐指数
1
解决办法
165
查看次数

标签 统计

c ×1

cs50 ×1

loops ×1

scope ×1

while-loop ×1