无法正确终止“while”循环

时间:2012-06-29 02:31:31

标签: c loops input while-loop terminate

我第一次尝试用C编程,然后将它应用到一些具体的东西......

我正在使用该问题创建的程序处理while循环。该计划的目标是计算一组卡车的每加仑平均英里数。我希望它一旦输入-1作为消耗的加仑数输出就终止,但我必须输入两次,一次输入加仑数,一次输入英里数。我发现这个输入实际上被用作计算结果的一部分。这是代码:

#include <stdio.h>
int main()
{

    int tanks, miles;
    float gallons = 0, average = 0, miles_per_gallon = 0;
    tanks = 0;

    while (gallons != -1) {

            tanks += 1;
            miles_per_gallon = (float)miles / gallons;
            average = average + miles_per_gallon;
            printf("The miles / gallon for this tank was %.3f\n",
                   miles_per_gallon);

            printf("Enter the gallons used (-1 to end): ");
            scanf("%f", &gallons);

            printf("Enter the miles driven: ");
            scanf("%d", &miles);
    }

    average /= tanks;
    printf("The overall average miles/gallon was %.3f", average);
    return 0;
}

以下是一些示例输出:

C:\>gallons
Enter the gallons used (-1 to end): 12.3
Enter the miles driven: 700
The miles / gallon for this tank was 56.911
Enter the gallons used (-1 to end): 13.4
Enter the miles driven: 666
The miles / gallon for this tank was 49.701
Enter the gallons used (-1 to end): 17.3
Enter the miles driven: 644
The miles / gallon for this tank was 37.225
Enter the gallons used (-1 to end): 15.5
Enter the miles driven: 777
The miles / gallon for this tank was 50.129
Enter the gallons used (-1 to end): -1
Enter the miles driven: -1
The miles / gallon for this tank was 1.000
The overall average miles/gallon was 38.993

感谢您的帮助。

4 个答案:

答案 0 :(得分:2)

问题是代码中的语句序列是这样的,直到请求第二个输入后才能检查循环的退出条件。您可以在输入后立即添加对-1的检查,并从循环中突破。或者,您可以要求在加仑之前输入里程。

for (;;) { /* This is a "forwver" loop; we break out from the middle */

        tanks += 1;
        miles_per_gallon = (float)miles / gallons;
        average = average + miles_per_gallon;
        printf("The miles / gallon for this tank was %.3f\n",
               miles_per_gallon);

        printf("Enter the gallons used (-1 to end): ");
        scanf("%f", &gallons);

        /* This is where you break from the loop: */
        if (gallons == -1) return 0;

        printf("Enter the miles driven: ");
        scanf("%d", &miles);
}

答案 1 :(得分:1)

那么你应该能够自己解决这个问题,如果你只是改变你的循环或者在加仑输入后加上if语句就很容易

while (gallons != -1) {

        tanks += 1;
        miles_per_gallon = ( float ) miles / gallons;
        average = average + miles_per_gallon;
        printf("The miles / gallon for this tank was %.3f\n", miles_per_gallon);

        printf("Enter the gallons used (-1 to end): ");
        scanf("%f", &gallons);

        if(gallons==-1){
             printf("Program terminated");
            return 0;
        }

        printf("Enter the miles driven: ");
        scanf("%d", &miles);
    }

答案 2 :(得分:1)

读取加仑后检查退出条件。我对你的代码做了一些改动 - 因为你只是在阅读加仑后才打破,我将while条件改为true。第二次我把你的测试改为&lt; = 0,好像输入了0,你除以0这将打破你的数学,而任何小于0的东西都没有意义。第三,我在读取值后更改了计算和报告,因此不要除以零

  while (1) {

    printf("Enter the gallons used (-1 to end): ");
    scanf("%f", &gallons);
    if(gallons <= 0) break;

    printf("Enter the miles driven: ");
    scanf("%d", &miles);

    tanks += 1;
    miles_per_gallon = ( float ) miles / gallons;
    average = average + miles_per_gallon;
    printf("The miles / gallon for this tank was %.3f\n", miles_per_gallon);
  }

答案 3 :(得分:0)

while (gallons != -1) {
        /* snip */

        printf("Enter the gallons used (-1 to end): ");
        scanf("%f", &gallons);

        printf("Enter the miles driven: ");
        scanf("%d", &miles);
}

请注意,在询问这两个问题后,您只评估退出条件。如果你想避免要求里程,你必须进一步扭曲你的循环。它看起来像这样:

while (gallons != -1) {
        /* snip */

        printf("Enter the gallons used (-1 to end): ");
        scanf("%f", &gallons);

        if (gallons == -1)
            break;  /* exit the while loop */

        printf("Enter the miles driven: ");
        scanf("%d", &miles);
}

当然,像这样退出中间的循环是bit rough around the edges,但我没有立即看到更方便的方法来终止循环而不问第二个问题。