【问题标题】:How do I add the same variable to itself?如何将相同的变量添加到自身?
【发布时间】:2016-10-15 03:39:40
【问题描述】:

我试图在 C 中为自身添加一个变量。我必须解决的问题需要我询问用户 X 发生了多少次 Y。简单的例子:每次有人喝果汁(x)时,我需要反复添加果汁(y)的量。 到目前为止,这是我的程序。我正在尝试做的一切都是按照我的知识预期工作,除了最后一段代码我需要弄清楚哪些需要在“if”语句之前。预先感谢您的协助。

#include <stdio.h>
int main(){
int a=1;
int b=1;
int i;
float dollars;
float size;
float price;//per ounceprice
float wieght;
int drinks;//times roommate took some juice
int c=0;
int sips;
int total;
int totalowed;
int loopCounter;
int sipstotal;
//.06 per ounce
float juiceTaken;
float juiceTakenCost;
float juiceTakenTotal;
float costperounce=.06;

while(a=b){
    printf("What is the weight (in oz.) of the original container of OJ?\n\n");
    scanf("%f", &wieght);

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

    printf("How many times did your roomate take your juice?\n\n");
    scanf("%d", &drinks);


        for(loopCounter = 0; loopCounter < drinks; loopCounter++){//repeat next line until loop equals amount of times roomate took juice
        printf("How much juice did your roommate take this time?\n\n");
        scanf("%d", &juiceTaken);
            if(juiceTakenTotal>=10)
            printf("Your roomate owes you $%.2f\n", juiceTakenTotal);



}
}

return 0;
}

【问题讨论】:

  • 谢谢你们!你们都提供了一个有效的解决方案!我还发现了代码的另一个问题,这使它在我最初尝试时无法正常工作。在我的 scanf 行中,当询问 juiceTaken 时,我记下 %d 而不是 %f。我正在扫描浮点值,同时要求整数值。大声笑,感谢您的帮助。

标签: c variables addition


【解决方案1】:

您的代码的问题在于,您从未在代码中添加果汁的总量。因此,就在最后一个 if 语句之前,您需要添加该行。它看起来像:

scanf("%d", &juiceTaken);
juiceTakenTotal = juiceTakenTotal + juiceTaken;
        if(juiceTakenTotal>=10)
        printf("Your roomate owes you $%.2f\n", juiceTakenTotal);

这将解决您当前的问题。

【讨论】:

    【解决方案2】:

    我相信这就是您正在寻找的:

        scanf("%d", &juiceTaken);
        juiceTakenTotal = juiceTakenTotal + juiceTaken;
                if(juiceTakenTotal>=10)
                price = juiceTakenTotal * price //calculating price
                printf("Your roomate owes you $%.2f\n", juiceTakenTotal);
    

    在这里,您将 sip 的数量添加到 sip 的总数中。

    【讨论】:

    • 我正在尝试在“if”之前但在“scanf”之后为 juiceTaken 执行此操作,以便将 juiceTaken 添加到自身以获得新的总数。
    • 思路是,如果“drink”的次数=10,它会反复询问juicetaken 10次。每次询问用户时,juicetaken 的值必须加起来。然后我将取的果汁总量乘以价格,并使用“if”来检查它是否大于或等于 10。
    • 您是要查看价格还是查看juiceTakenTotal?
    • 我也添加了价格位。
    【解决方案3】:

    这是正确和最终的代码。感谢您所有的帮助!天才满分!

    #include <stdio.h>
    int main(){
    int a=1;
    int b=1;
    float dollars;
    float price;//per ounceprice
    float wieght;
    int drinks;//times roommate took some juice
    int loopCounter;
    float juiceTaken;
    float juiceTakenCost;
    float juiceTakenTotal;
    
    while(a=b){
        printf("What is the weight (in oz.) of the original container of OJ?\n\n");
        scanf("%f", &wieght);
    
        printf("What is the cost of the original container of OJ in dollars?\n\n");
        scanf("%f", &dollars);
        price=dollars/wieght;
        printf("%f\n\n", price);
    
        printf("How many times did your roomate take your juice?\n\n");
        scanf("%d", &drinks);
        //6 cents per ounce of juice 
        //Insert 2nd loop condition - for loop
            for(loopCounter = 0; loopCounter < drinks; loopCounter++){//repeat next line until loop equals amount of times roomate took juice
            printf("How much juice did your roommate take this time?\n\n");
            scanf("%f", &juiceTaken);
            juiceTakenTotal=juiceTakenTotal + juiceTaken;
            juiceTakenCost=juiceTakenTotal*price;
                if(juiceTakenCost>=10)
                printf("Your roomate owes you $%.2f\n", juiceTakenCost);
                }
        }
    return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-16
      • 2022-07-29
      • 2022-06-15
      • 2018-12-08
      • 2012-07-24
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      相关资源
      最近更新 更多