我想什麼就會在躲不過前言本我非常新的節目,並有很多值得學習。所以我有一個問題,我的嵌套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