#include
#include
int main(void)
{
double cost=0.0, kws=0.0; //变量使用前初始化
printf("Enter Number:");
scanf("%lf", &kws);
while (kws < 0) //当输入值<0,打印提示信息,并重新输入值,直到输入正确
{
printf("Invalid Value!\n");
printf("Enter Number:");
scanf("%lf", &kws);
}
if (kws <= 50){
cost = 0.53*kws;
}
else{
cost = 0.53 * 50 + 0.58*(kws - 50);
}
printf("cost = %.2f\n", cost);
system("PAUSE"); //用来暂停console端
return 0;
}
程序里没有判断是不是小于0。
可以这么写:
if(cost<0.01)
printf("Invalid Value!\n");
else
printf("cost = %.2f\n",cost);