我試圖在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。我在詢問整數值時正在掃描浮點值。大聲笑,我感謝你的幫助球員。 –