if else语句,while循环中断

时间:2015-02-07 22:17:20

标签: loops if-statement while-loop nested

我想在此之前加上一些显而易见的事情,因为我对编程非常陌生并且需要学习很多东西。所以我遇到了一个问题,我的嵌套if else语句被忽略了。第二个和第三个“printf”语句是整数,但会转换为小数,因为它们是应用于中型和大型订单价格的折扣百分比。我正在使用while循环来测试3个条件,如果它们都满足则打印消息,但是如果它们不符合,我已经制作了3个if和if else语句来测试哪个条件没有得到满足。但是,由于没有准确打印,我错过了一些东西。基本小部件成本不应高于基本价格,药品价格或大价格,因为这会使小部件亏本出售。我不确定我做错了什么,除了我太混淆的令人困惑的名字。

#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;
}

1 个答案:

答案 0 :(得分:0)

您应该将'while'更改为'if'。否则你的while将会永远运行,并且在那个while语句之后你永远不会得到代码。虽然意味着“只要条件成立,重复此操作”,并且您不希望它重复。

您还应该将所有'else if'更改为'if',因为根据您的描述,您希望Nick知道所有亏钱的定价点。因此,如果med_price和卖价都太低,您想要打印两个警告语句,而不仅仅是第一个。否则,如果遇到先前的if或else if条件,将永远不会看到案例。

另外一件事......既然你在所有情况下都做'printf(“祝你好运!\ n”)',你只需要在'返回0'正上方一行。删除该语句的所有其他实例。

祝你好运!

相关问题