正确使用 do while 循环?
Using a do while loop correctly?
我的条件如下:
- 如果 userIncome 输入的值超过 400000,则显示错误。
- 同理,如果小于500,报错。
重复循环直到输入正确的值。
#define _CRT_SECURE_NO_WARNINGS
#define minINCOME 500.00
#define maxINCOME 400000.00
#include <stdio.h>
int main(void)
{
float userIncome ;
printf("+--------------------------+\n"
"+ Wish List Forecaster |\n"
"+--------------------------+\n");
do
{
printf("Enter your monthly NET income: $");
scanf(" %.2lf", &userIncome);
if (userIncome < 500)
{
printf("ERROR: You must have a consistent monthly income of at least 0.00\n");
}
if (userIncome > 40000)
{
printf("ERROR: Liar! I'll believe you if you enter a value no more than 0000.00\n");
}
} while (userIncome >= maxINCOME && userIncome <= minINCOME);
return 0;
}
对于初学者来说,这个命名常量
#define maxINCOME 400000.00
与此 if 语句中使用的常量不同
if (userIncome > 40000)
在这种情况下
while (userIncome >= maxINCOME && userIncome <= minINCOME);
其次对于像这样的 float 类型的对象
float userIncome ;
您需要使用转换说明符 f
而不是 lf
scanf(" %f", &userIncome);
而且条件应该这样写
while (userIncome >= maxINCOME || userIncome <= minINCOME);
并且在 if 语句中,您需要使用定义的命名常量,例如
if (userIncome >= maxINCOME)
^^
我的条件如下:
- 如果 userIncome 输入的值超过 400000,则显示错误。
- 同理,如果小于500,报错。
重复循环直到输入正确的值。
#define _CRT_SECURE_NO_WARNINGS
#define minINCOME 500.00
#define maxINCOME 400000.00
#include <stdio.h>
int main(void)
{
float userIncome ;
printf("+--------------------------+\n"
"+ Wish List Forecaster |\n"
"+--------------------------+\n");
do
{
printf("Enter your monthly NET income: $");
scanf(" %.2lf", &userIncome);
if (userIncome < 500)
{
printf("ERROR: You must have a consistent monthly income of at least 0.00\n");
}
if (userIncome > 40000)
{
printf("ERROR: Liar! I'll believe you if you enter a value no more than 0000.00\n");
}
} while (userIncome >= maxINCOME && userIncome <= minINCOME);
return 0;
}
对于初学者来说,这个命名常量
#define maxINCOME 400000.00
与此 if 语句中使用的常量不同
if (userIncome > 40000)
在这种情况下
while (userIncome >= maxINCOME && userIncome <= minINCOME);
其次对于像这样的 float 类型的对象
float userIncome ;
您需要使用转换说明符 f
而不是 lf
scanf(" %f", &userIncome);
而且条件应该这样写
while (userIncome >= maxINCOME || userIncome <= minINCOME);
并且在 if 语句中,您需要使用定义的命名常量,例如
if (userIncome >= maxINCOME)
^^