Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
4年前关闭。
如果我使用float或double而不使用int,为什么此代码给出的答案完全错误?
至
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
4年前关闭。
如果我使用float或double而不使用int,为什么此代码给出的答案完全错误?
//C How to Program Exercises 2.33
#include <stdio.h>
#include <conio.h>
double dailyDrivingCost(double miles, double costPerGallon,double averageMilesPerGallon);
int main(void){
double a,b,c,d,e;
printf ("Please enter the number of miles daily\n");
scanf("%lf",&a);
printf ("Please enter the cost per gallon\n");
scanf("%lf",&b);
printf ("Please enter the average miles per gallon\n");
scanf("%lf",&c);
printf ("The number of miles per gallon is:%d",dailyDrivingCost(a,b,c));
getch();
return 0;
}
double dailyDrivingCost(double miles, double costPerGallon,double averageMilesPerGallon){
double overall_cost;
overall_cost= (miles/averageMilesPerGallon)+ costPerGallon;
return overall_cost;
}
最佳答案
更改
printf ("The number of miles per gallon is:%d",dailyDrivingCost(a,b,c));
^
至
printf ("The number of miles per gallon is:%f",dailyDrivingCost(a,b,c));
d
转换说明符用于打印int
,要打印double
(或float
),则需要f
转换说明符。