2016-04-28 74 views
-3

我剛剛開始學習C.因此,對於實踐我決定寫一個利率計算器,但由於某種原因,我的計算總是作爲0出來,我無法弄清楚。如果你可以請看看。利率計劃

#include <stdio.h> 


main() 
{ 


int time; 
float principle, rate, total; 

printf("What is your starting amount? "); 
scanf(" %f", &principle); 
printf("What is your interest rate? "); 
scanf(" %f", &rate); 
printf("How long do you want to save? "); 
scanf(" %d", &time); 

total = principle * rate * time; 
printf("You will have earned an interest amount of $%d", total); 
return 0; 
} 
+2

'%d'期望'int',而不是'float'。 –

+1

哦,杜,謝謝我很抱歉浪費你的時間。 –

+0

[演示](http://ideone.com/fMnqhN) –

回答

1
printf("You will have earned an interest amount of $%d", total); 

由於total是浮點數,%d%f。此外,在每一行的末尾添加一條新線是一個不錯的選擇。

printf("You will have earned an interest amount of $%f\n", total);