如何将相同的变量添加到自身?
How do I add the same variable to itself?
我正在尝试在 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("%d", &juiceTaken);
juiceTakenTotal = juiceTakenTotal + juiceTaken;
if(juiceTakenTotal>=10)
price = juiceTakenTotal * price //calculating price
printf("Your roomate owes you $%.2f\n", juiceTakenTotal);
在这里,您要将啜饮的次数添加到饮饮的总数中。
您的代码存在的问题是您从未在代码中将榨汁量添加到总量中。因此,就在最后一个 if 语句之前,您需要添加该行。它看起来像:
scanf("%d", &juiceTaken);
juiceTakenTotal = juiceTakenTotal + juiceTaken;
if(juiceTakenTotal>=10)
printf("Your roomate owes you $%.2f\n", juiceTakenTotal);
这将解决您当前的问题。
这是正确的最终代码。感谢您所有的帮助!到处都是天才!
#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;
}
我正在尝试在 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("%d", &juiceTaken);
juiceTakenTotal = juiceTakenTotal + juiceTaken;
if(juiceTakenTotal>=10)
price = juiceTakenTotal * price //calculating price
printf("Your roomate owes you $%.2f\n", juiceTakenTotal);
在这里,您要将啜饮的次数添加到饮饮的总数中。
您的代码存在的问题是您从未在代码中将榨汁量添加到总量中。因此,就在最后一个 if 语句之前,您需要添加该行。它看起来像:
scanf("%d", &juiceTaken);
juiceTakenTotal = juiceTakenTotal + juiceTaken;
if(juiceTakenTotal>=10)
printf("Your roomate owes you $%.2f\n", juiceTakenTotal);
这将解决您当前的问题。
这是正确的最终代码。感谢您所有的帮助!到处都是天才!
#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;
}