gpt4 book ai didi

c - 如何将相同的变量添加到自身?

转载 作者:行者123 更新时间:2023-11-30 16:56:07 25 4
gpt4 key购买 nike

我正在尝试在 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;
}

最佳答案

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

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

这将解决您当前的问题。

关于c - 如何将相同的变量添加到自身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40054695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com