平均计算器 - 为什么不“var + = var;”嵌套在while循环中工作?

时间:2012-05-18 07:16:31

标签: c variables average

我对Stack Overflow和C编程都很新,希望我不会惹恼任何缺乏知识的人。

我正在为Kickstarter项目创建一个平均计算器,我想知道为什么下面的方法不起作用。不是平均水平,但如果你每天投入1个支持者和10美元的承诺,为什么支持者和资金会增加一倍,

    #include <stdio.h>
    #include <conio.h>

    int main(void){

    int loopcount = 0;
    int backers = 0;
    int money = 0;
    int average = 0;

    int tbackers = 0;
    int tmoney = 0;

    while(loopcount<5){

       //Ask for # of backers and total money pledged.
       printf("Please Enter the number of backers today, then the total money pledged                                today:\n");
       scanf("%d\n%d", &backers, &money);

       //
       backers += backers;
       money += money;


       loopcount++;
        }
       //average = tmoney / tbackers;
       printf("There were %d backers and the total collected was $%d.\nSo the average amount pledged was  $%d", backers, money, average);

       getch();

}

但以下工作正常

#include <stdio.h>
#include <conio.h>

int main(void){

    int loopcount = 0;
    int backers = 0;
    int money = 0;
    int average = 0;

    int tbackers = 0;
    int tmoney = 0;

    while(loopcount<5){

       //Ask for # of backers and total money pledged.
       printf("Please Enter the number of backers today, then the total money pledged today:\n");
       scanf("%d\n%d", &backers, &money);

       //
       tbackers += backers;
       tmoney += money;


       loopcount++;
    }
       //average = tmoney / tbackers;
       printf("There were %d backers and the total collected was $%d.\nSo the average amount pledged was  $%d", tbackers, tmoney, average);

       getch();

}

2 个答案:

答案 0 :(得分:7)

在第一种情况下,在每次计算后,您在获得新用户输入时覆盖该值:

scanf("%d\n%d", &backers, &money);

例如:

line                  | backers    | money
----------------------+------------+------
user input 5 3        | 5          | 3
backers += backers    | 10         | 3
money += money        | 10         | 6
user input 8 6        | 8          | 6
backers += backers    | 16         | 6
money += money        | 16         | 12

现在,在第二个示例中,您不会覆盖值,而是将它们添加到总和中:

line                  | backers    | money    | tbackers   | tmoney    
----------------------+------------+----------+------------+---------
user input 5 3        | 5          | 3        | 0          | 0
tbackers += backers   | 5          | 3        | 5          | 0
tmoney += money       | 5          | 3        | 5          | 3
user input 8 6        | 8          | 6        | 5          | 3
tbackers += backers   | 8          | 6        | 13         | 3
tmoney += money       | 8          | 6        | 13         | 9

答案 1 :(得分:1)

什么“不起作用”究竟是什么意思?我无法想象它不起作用。

money += moneymoney的值添加到变量money的内容中,实际上将其值加倍。

如果你想要实现别的东西,你必须像第二个例子那样表达别的东西:有一个总和的变量和一个用户输入的变量。如果你混淆它们,你最终会被破坏(覆盖)的数据。

如果你在C99或者你的编译器允许它,你可以声明你的变量接近你需要的位置:

[...]

while(loopcount<5){
   int money = 0;
   int average = 0;


   //Ask for # of backers and total money pledged.
   printf("Please Enter the number of backers today, then the total money pledged today:\n");
   scanf("%d\n%d", &backers, &money);

   //
   tbackers += backers;
   tmoney += money;

   loopcount++;
}
[...]

这限制了backersmoneywhile循环范围内的使用,因此它们无法泄漏。