我想什么就会在躲不过前言本我非常新的节目,并有很多值得学习。所以我有一个问题,我的嵌套if else语句被忽略。第二个和第三个“printf”语句是整数,但转换为小数,因为它们是适用于大中型订单价格的百分比折扣。我正在使用while循环来测试3个条件,如果它们全部满足,则打印该消息,但是如果它们不符合,我已经做了3条if和if条件语句来测试哪些条件未被满足。然而,我错过了一些东西,因为它们没有被准确地打印出来。基础小部件的成本不应高于基本价格,医药价格或者大价格,因为这会使小部件亏本销售。我不确定我做错了什么,除了我那些太类似的混淆名字。如果else语句,while循环破碎
#include <math.h>
#include <stdio.h>
#define widget_cost 0.40
int main(){
float sellprice;
int discount_med;
float discount_med_price;
float med_price;
float large_price;
int discount_large;
float discount_large_price;
printf("How much are you selling each widget for\n");
scanf("%f", &sellprice);
printf("What are you discounting medium orders?\n");
scanf("%f", &discount_med);
printf("What are you discounting large orders?\n");
scanf("%f", &discount_large);
discount_med_price=discount_med/100 * sellprice;
med_price= sellprice-discount_med_price;
discount_large_price=discount_large/100 * sellprice;
large_price=sellprice-discount_large_price;
while (sellprice>widget_cost && med_price>widget_cost && large_price>widget_cost){
printf("All prices look good to me!\n");
printf("Good luck!\n");
}
if (widget_cost>sellprice){
printf("Nick, your base price is too low!");
printf("Good luck!\n");
}
else if (widget_cost>med_price){
printf("Nick, you are discounting medium purchases too much!\n");
printf("Good luck!\n");
}
else if (widget_cost>large_price){
printf("Nick, you are discounting large purchases too much!\n");
printf("Goof luck!\n");
}
return 0;
}
discount_med声明int类型的,但你用在scanf函数说明符'%F'。另外,除法将用int来截断。 – 2015-02-07 22:27:38
你真的需要while循环吗?如果满足所有三个条件,那么您将打印“所有价格对我来说都很好”,因为一段时间条件永远不会变成错误,并且您将陷入无限循环。我相信你应该把它改成if语句。 – jadhachem 2015-02-07 22:29:34
是的,我刚刚意识到这一点,我已经将它改为if语句,因为如果全部符合,它会打印一次。 – Phlash6 2015-02-07 22:32:51