汽车加油问题 着急,知道的帮下忙
下面是汽车加油的一个问题,读入行驶的总公里数和每次加满油后使用的燃料数,计算并显示每次加油后的每加仑平均行驶的里程数,然后还要计算并显示所有燃料的每加仑平均行驶的里程数,下边是我写的程序:#include <stdio.h>int main ()
{
int miles;
int total;
int counter;
float gallons;
float s;
float average;
total=0;
counter=0;
printf ("Enter the gallons used (-1 to end):");
scanf ("%f",&gallons);
printf ("Enter the miles driven:");
scanf ("%d",&miles);
s = (float) miles / gallons;
printf ("The miles / gallon for this tank was %f\n\n",s);
while (gallons!=-1)
{
counter += gallons;
total += miles;
printf ("Enter the gallons used (-1 to end):");
scanf ("%f",&gallons);
if (gallons!=-1)
{
printf ("Enter the miles driven:");
scanf ("%d",&miles);
s = (float) miles / gallons;
printf ("The miles / gallon for this tank was %f\n\n",s);
}
else
{
average = (float) total / counter;
printf ("\nThe overall average miles/gallon was %f\n",average);
}
}
}
别的都对,就是最后计算平均数的时候,有小数点就不对,别的都没问题,哪位知道,帮帮忙,看看那里错了
[[it] 本帖最后由 wangyang916 于 2008-12-9 15:46 编辑 [/it]]