2

我第一次尝试用 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

4 回答 4

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);
}
于 2012-06-29T02:46:53.170 回答
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);
    }
于 2012-06-29T02:46:41.940 回答
1

读取加仑数后检查退出条件。我对您的代码进行了一些更改 --- 因为您只是在阅读加仑后才中断,所以我将 while 条件更改为 true。其次,我将您的测试更改为 <=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);
  }
于 2012-06-29T02:51:57.463 回答
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);
}

当然,像这样在中间退出循环有点粗糙,但我没有立即看到更方便的方法来终止循环而不问第二个问题。

于 2012-06-29T02:50:34.587 回答