简体   繁体   English

我无法结束C中的循环

[英]I'm having trouble ending a loop in C

I'm in school learning C. (I am not asking for anyone to write this for me). 我在学校学习C。(我不要求任何人为我写这篇文章)。

Assignment 分配
This program will calculate the miles per gallon MPG for you for three tanks of gas after you have entered the gallons used and miles driven. 输入您使用的加仑数和行驶的英里数后,此程序将为您计算三个汽油箱的英里/加仑MPG

I can get my program to start a loop, but I can't figure out how to make it end the loop after 3 runs and give me the Average MPG in 3 tanks. 我可以让我的程序开始循环,但是我无法弄清楚如何使其在运行3次后结束循环并为我提供3辆坦克的平均MPG。 Running the program give me the average, but will keep asking forever. 运行该程序可以得到平均水平,但会一直询问下去。

#include <stdio.h>
int main(void) {
int miles;
float gallons = -1, mg, overall = 0, avg = 0;
while(gallons != 0) {
  printf("Enter the gallons used: ");
  scanf("%f", &gallons);
  if (gallons == 0) {
    printf("\n\n The overall miles/gallon was: %f\n", overall/avg);
    exit(0);
    }
  printf("Enter miles driven: ");
  scanf("%d", &miles);
  mg = miles/gallons;
  printf("The miles/gallon for this tank was : %f\n", mg);
  overall += miles;
  avg += gallons;
}
  return 0;
}

Try this small changes. 试试这个小的变化。 Use an iterator to get average of 3 tanks. 使用迭代器平均获得3个战车。

Modify like 修改像

i=0;
while(i < 3) {
i++;

#include <stdio.h>
int main(void) {
int miles, **i=0;**  
float gallons = -1, mg, overall = 0, avg = 0; 
**while(i < 3)** {
  printf("Enter the gallons used: ");
  scanf("%f", &gallons);
  if (gallons == 0) {
    printf("\n\n The overall miles/gallon was: %f\n", overall/avg);
    exit(0);
    }
  printf("Enter miles driven: ");
  scanf("%d", &miles);
  mg = miles/gallons;
  printf("The miles/gallon for this tank was : %f\n", mg);
  overall += miles;
  avg += gallons;
  **i++;**
}
  return 0;
}

Sorry : I did not see how is gallons being assigned/initialized in your code, I saw float galons and while (gallons != 0) and then thought that gallons was at some point the result of a computation. 抱歉 :我没有看到gallons是如何在您的代码中分配/初始化的,我看到了float galons while (gallons != 0)while (gallons != 0) ,然后认为gallons在某种程度上是计算结果。

This answer is still useful in my opinion. 我认为这个答案仍然有用。

Don't use float values to check conditions, float s are not accurate because their machine representation cannot be, so gallons != 0 will probably hardly ever be true, use int instead and your loop control will work correctly. 不要使用float值检查条件,因为它们的机器表示不正确,所以float s是不准确的,因此gallons != 0几乎不可能是真实的,而是使用int并且循环控制将正常工作。 Only use float for the average value. 仅将float用作平均值。

But in fact, because your specific problem can be solved with a for loop, you should use 但实际上,由于可以使用for循环解决特定的问题,因此应使用

for (int i = 0 ; i < 3 ; ++i)

instead, that way you know that it will only loop 3 times. 而是,您知道它只会循环3次。

SIDE NOTE : learn more about scanf() and why you MUST check the value that it retuns in programs like yours. 旁注 :了解有关scanf()更多信息,以及为什么必须检查它在诸如您的程序中重新调整的值的原因。

Your program, as written, does not stop at 3 tanks. 如您所写,您的程序不会停在3辆战车上。 It will continuously ask for tanks until you answer 0 to the number of gallons used. 它将持续询问油箱,直到您对使用的加仑数回答0。

To make it read at most three tanks, replace while (gallons != 0) with for (int i = 0; i < 3; i++) . 要使其最多读取三个水箱,请将for (int i = 0; i < 3; i++)替换while (gallons != 0) for (int i = 0; i < 3; i++) That will make the main loop run three times only. 这将使主循环仅运行3次。

But then it won't print the overall average. 但是,那样就不会打印出总体平均值。 It will simply quit after running three times. 运行3次后,它只会退出。 The code that shows the overall average is inside that if test that checks if you typed 0 gallons. 显示总体平均值的代码在if测试中,该测试检查您是否键入了0加仑。 Remove that if test and move the printf statement which shows the overall average near the end of the program, right before the return statement. if测试, if删除,然后将显示总体平均水平的printf语句移至程序结尾附近,就在return语句之前。 This way it will run after the for loop runs 3 times. 这样,它将在for循环运行3次之后运行。

float gallons = -1;

It doesn't make any sense; 这没有任何意义; And you need to notice one thing that is 您需要注意一件事

while(gallons!=0){
  //code
 }

You are asking the user to enter the value if gallons to input and your not changing it's value so this value will always be true in while and loop will go infinite. 您正在要求用户输入要输入的加仑值,并且不更改它的值,因此该值在while期间将始终为true,并且循环将变为无限。

If you need to run the loop three times then you can do it by using variable. 如果需要运行三次循环,则可以使用变量来执行。 ` `

int i=3;
while(i>0){//code 
i--;
}

Here I have edited your program ; 我在这里编辑了你的程序;

#include <stdio.h>
int main(void) {
int miles,i=3;
float gallons, mg, overall = 0, avg = 0;
while(i>0) {
printf("Enter the gallons used: ");
scanf("%f", &gallons);
if (gallons == 0) {
   printf("\n\n The overall miles/gallon was: %f\n", overall/avg);
 exit(0);
   }
 printf("Enter miles driven: ");
 scanf("%d", &miles);
 mg = miles/gallons;
 printf("The miles/gallon for this tank was : %f\n", mg);
 overall += miles;
 avg += gallons;i--;
    }
 return 0;
 }

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM