我知道我应该自己调试。。。但相信我,我试过了,我很尴尬。我不明白为什么我的while循环是无限循环的。有人能帮忙吗?
#include <stdio.h>
int main ( void )
{
double milesDriven;
double gallonsUsed;
double totalMilesDriven;
double totalGallonsUsed;
float milesPerGallon;
float totalMpG;
printf( "%s", " Enter the gallons used (-1 to end): " );
scanf( "%i", &gallonsUsed);
printf( " Enter the miles driven: " );
scanf( "%i", &milesDriven);
while ( gallonsUsed != -1 || milesDriven != -1)
{
totalGallonsUsed += gallonsUsed;
totalMilesDriven += milesDriven;
milesPerGallon = ( milesDriven / gallonsUsed );
printf( " The miles/gallon for this tank was %f\n", milesPerGallon );
printf( "%s", " Enter the gallons used (-1 to end): " );
scanf( "%i", &gallonsUsed);
printf( " Enter the miles driven: " );
scanf( "%i", &milesDriven);
}
totalMpG = ( totalMilesDriven / totalGallonsUsed );
printf( " The overall average miles/gallon was %.6f\n ", totalMpG);
return 0;
}
最佳答案
乍一看,似乎您使用的是浮点数据类型,而您应该使用整数。
"%i" // expects an integer
尝试使用
int
数据类型,或将格式更改为"%lf"
关于c - 简单的无限while循环-c,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18862318/