2016-09-15 47 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

这工程!非常感谢你! –