【发布时间】: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。我正在扫描浮点值,同时要求整数值。大声笑,感谢您的帮助。