我刚刚开始学习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;
}
答案 0 :(得分: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);