gpt4 book ai didi

c - 简单的无限 while 循环-- c

转载 作者:太空宇宙 更新时间:2023-11-04 06:32:48 25 4
gpt4 key购买 nike

我知道我应该自己调试它...但请相信我,我已经尝试过了,但我感到非常尴尬。我不明白为什么我的 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;
}

最佳答案

乍一看,您似乎在使用 float 据类型,而您应该使用整数。

"%i" // expects an integer

尝试使用 int 数据类型,或将格式更改为 "%lf"

关于c - 简单的无限 while 循环-- c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18862318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com