2016-09-15 46 views
0

我很難弄清楚如何讓它運行/放置不同的方程式。分配在代碼下面。如何完成循環?

#include <stdio.h> 

#define OJ 10 


//main function 
int main() { 

    int amount_taken, weight, times_taken, cost_per_oz, num_times; 
    float cost_of_container, leftovers, total_cost; 

    printf("What is the weight (in oz.) of the original container of OJ?\n"); 
    scanf("%d", &weight); 

    printf("What is the cost of the original container of OJ in dollars?\n"); 
    scanf("%f", &cost_of_container); 

    printf("How many times did your roommate take your juice?\n"); 
    scanf("%d", &times_taken); 
    for(num_times = 0; num_times < times_taken; num_times++) { 
.  while (total_cost <= OJ) { 

       printf("How much juice did your roommate take this time (in oz.)?\n"); 
       scanf("%d", &amount_taken); 
       total_cost = cost_per_oz * amount_taken; 
       cost_per_oz = cost_of_container/weight; 

      if (total_cost >= OJ) { 
       printf("Your roommate owes you $10.00.\n"); 
       total_cost = total_cost - 10; 
      }//if 

      printf("How much juice did your roommate take this time (in oz.)?\n"); 
      scanf("%d", &amount_taken); 

     }// while 

    } //for 

    leftovers = total_cost - 10; 
    printf("Your roommate owes you $%.2f.\n", leftovers); 

    return 0; 
} 

你和你的室友經歷了大量的橙汁。你已經注意到你的室友比你更多地喝橙汁。當他們用完時,他們會拿走你的一些。

你一直在爲你的室友收取每次他們的橙汁。但從他們那裏收集每次50或75美分感覺有點愚蠢。你已經提出了一個輝煌的計劃!他們不是每次拿橙汁都要給他們充電,而是在他們購買了價值10美元的果汁後從他們那裏收錢。

程序安裝:

編寫一個程序來模擬這個過程。要求用戶輸入您購買的果汁容器的大小(以盎司爲單位)以及這些容器的價格(以美元計)。然後提示用戶輸入室友喝多少次果汁。最後,請閱讀室友每次參加的金額。每當果汁的總價值等於或超過10美元時,打印出「你的室友欠你10美元」。輸入所有數字後,如果室友欠下任何錢,就打印出所欠的價值。

樣品試驗

What is the weight (in oz.) of the original container of OJ? 
64 
What is the cost of the original container of OJ in dollars? 
3.79 
How many times did your roommate take your juice? 
10 
How much juice did your roommate take this time (in oz.)? 
30 
How much juice did your roommate take this time (in oz.)? 
34 
How much juice did your roommate take this time (in oz.)? 
24 
How much juice did your roommate take this time (in oz.)? 
40 
How much juice did your roommate take this time (in oz.)? 
64 
Your roommate owes you $10.00. 
How much juice did your roommate take this time (in oz.)? 
64 
How much juice did your roommate take this time (in oz.)? 
64 
How much juice did your roommate take this time (in oz.)? 
18 
Your roommate owes you $10.00. 
How much juice did your roommate take this time (in oz.)? 
20 
How much juice did your roommate take this time (in oz.)? 
20 
Your roommate owes you $2.38. 
+0

開始使用它之前初始化'total_cost'和定義該函數''中掃描scan'使用( 「%d」,&amount_taken);'或更改爲'的scanf '。 – MikeCAT

+0

這會在while語句中,在for語句中,還是在兩者之外? –

+0

當你聲明變量時,它會在一開始就進行。 – Barmar

回答

1

請嘗試,如果這個代碼可以幫助你。這些改變不是很多,現在它似乎打印出正確的輸出。

#include <stdio.h> 
#include <time.h> 
#include <math.h> 
#define OJ 10 
int main() { 
    float amount_taken, weight, cost_per_oz; 
    int times_taken, num_times; 
    float cost_of_container, total_cost = 0; 

    printf("What is the weight (in oz.) of the original container of OJ?\n"); 
    scanf("%f", &weight); 
    printf("What is the cost of the original container of OJ in dollars?\n"); 
    scanf("%f", &cost_of_container); 
    cost_per_oz = cost_of_container/weight; 
    printf("How many times did your roommate take your juice?\n"); 
    scanf("%d", &times_taken); 
    for (num_times = 0; num_times < times_taken; num_times++) { 
      printf("How much juice did your roommate take this time (in oz.)?\n"); 
      scanf("%f", &amount_taken); 
      total_cost += cost_per_oz * amount_taken; 
      if (total_cost >= OJ) { 
       printf("Your roommate owes you $10.00.\n"); 
       total_cost = total_cost - 10; 
      } 
    } 
    printf("Your roommate owes you $%.2f.\n", total_cost); 
    return 0; 
} 

測試

What is the weight (in oz.) of the original container of OJ? 
10 
What is the cost of the original container of OJ in dollars? 
10 
How many times did your roommate take your juice? 
3 
How much juice did your roommate take this time (in oz.)? 
9 
How much juice did your roommate take this time (in oz.)? 
2 
Your roommate owes you $10.00. 
How much juice did your roommate take this time (in oz.)? 
2 
Your roommate owes you $3.00. 
+0

這工程!非常感謝你! –