私はCで1つの変数を追加しようとしています。解決しなければならない問題は、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を要求するときに%fの代わりに%dに注意します。私は整数値を求めて浮動小数点値をスキャンしています。笑、助けてくれてありがとう。 –