2015-02-07 187 views
0

我想什么就会在躲不过前言本我非常新的节目,并有很多值得学习。所以我有一个问题,我的嵌套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; 
} 
+0

discount_med声明int类型的,但你用在scanf函数说明符'%F'。另外,除法将用int来截断。 – 2015-02-07 22:27:38

+0

你真的需要while循环吗?如果满足所有三个条件,那么您将打印“所有价格对我来说都很好”,因为一段时间条件永远不会变成错误,并且您将陷入无限循环。我相信你应该把它改成if语句。 – jadhachem 2015-02-07 22:29:34

+0

是的,我刚刚意识到这一点,我已经将它改为if语句,因为如果全部符合,它会打印一次。 – Phlash6 2015-02-07 22:32:51

回答

0

您应该将'while'改为'if'。否则,你的时间将永远运行,并且在声明之后你永远不会得到代码。虽然意味着“只要条件成立,就重复这一行动”,而你不希望它重复。

你也应该改变你的所有“否则,如果”只是“如果”,因为从你的描述你想尼克知道赔钱所有价格点。因此,如果med_price和卖出价格都太低,您希望打印两个警告语句,而不仅仅是第一个。否则,如果情况将永远不会被看到,如果事先如果或者如果条件得到满足。

而另一件事情......既然你做一个“的printf(‘祝你好运!\ n’)”在任何情况下,你只需要一次正上方的“返回0”这条线。删除该语句的所有其他实例。

祝你好运!

+0

我已经改变了语句只的“如果”,然而当我测试样的条件,基价2,中值折价80,和大优惠90,我仍然得到了第一个条件满足,当它应该告诉我,大折扣太大。 – Phlash6 2015-02-07 22:53:28

+0

在上述条件下(基本价格,2美元折扣80%,大折扣90%,我的药物折扣是2折扣的1.6(80%),所以$ .40,这很好,但是平均折扣90% (90%)低于2美元的基数,即0.20美元,这太低了,并且应该触发第4条if语句,尼克你大量购买大额购买!但是当我在程序中运行这个时,我。我触发第一个if语句 – Phlash6 2015-02-07 22:57:28

+0

我可能不会使用#define语句为widget_cost我会把'漂浮widget_cost = 0.40;在主'某处()之类的其他变量声明后,我不能对此进行测试的权利现在但也许这会有所不同,也许你应该在计算完毕后打印所有的值,这样就可以确定这些值是你认为的值。就像'printf(“large_price是%f。\ n”,large_price) ;' – tdub 2015-02-07 23:19:07