2016-10-15 45 views
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; 
} 
+0

謝謝你們兩位!你們都提供了一個可行的解決方案!我在代碼中發現了另一個問題,當我最初嘗試時,它使它無法工作。在我的scanf行中,當詢問juiceTaken時,我注意到%d而不是%f。我在詢問整數值時正在掃描浮點值。大聲笑,我感謝你的幫助球員。 –

回答

0

您的代碼存在的問題是您從未在您的代碼中添加過量的果汁。因此,在最後一條if語句之前,您需要添加該行。它會看起來像:

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

這將解決您當前的問題。

0

我相信這是你在找什麼:

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

在這裏,你將啜飲的數量採取啜飲的總數。

+0

我正在嘗試在「if」之前的行中執行該操作,但在juiceTaken的「scanf」之後執行此操作,以便將juiceTaken添加到自身中以獲得新的總計。 –

+0

這個想法是,如果「飲料」的數量= 10,它會反覆詢問10次。每次詢問用戶時,juicetaken的值必須加總。然後,我將乘以價格總額juicetaken,並使用「if」來檢查它是否大於或等於10. –

+0

您打算檢查價格還是juiceTakenTotal? – Fabulous

0

這裏是正確和最終的代碼人。感謝您所有的幫助!天才全!

#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; 
} 
相關問題