我最初尝试使用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
重新分配值.