【问题标题】:Can someone point out a small logic error to me?有人可以向我指出一个小的逻辑错误吗?
【发布时间】:2014-06-24 11:19:11
【问题描述】:

我正在根据书本执行以下程序,但不明白我哪里出了问题。有人可以向我指出我遗漏的一些逻辑错误吗?

开发一个程序,输入每罐的行驶里程和加仑数。 该程序应计算并显示每加仑油所获得的英里数。处理完所有输入信息后,程序应计算并打印所有满罐的每加仑英里数。

#include <stdio.h>

int main(void) {
    int total = 0, count = 0;
    float gallons_used, mpg, miles;
    while(gallons_used != -1) {
    printf("Enter the gallons used (-1 to end): ");
        scanf("%f", &gallons_used);
        printf("Enter the miles driven: ");
        scanf("%f", &miles);
        mpg = miles / gallons_used;
        printf("Miles / gallon for this tank was %f\n", mpg);
        total += mpg;
        count++;
    }
    total /= count;
    printf("Average miles to the gallon was: %d\n", total);
    return 0;
}

现在,看来我的循环恰到好处,直到我以 -1 的值退出它,因为它仍然要求该油箱的里程,显然输入它会完全抛出总和结束。

【问题讨论】:

  • 那是因为你没有在从用户那里读取到 gallons_used 之后就退出。在此之后放置一个 break 语句,而不是在 while 条件中。
  • 您检查了gallons_used 的值,但您从未初始化它!如果它恰好以值 -1 开头怎么办??
  • 或者至少只检查gallons_used &lt; 0,如果是,则跳出循环
  • 我不推荐这种与浮点类型的比较......它有时会给你带来麻烦,在某些情况下将浮点数与整数进行比较可能会导致一些错误,通常在使用浮点数比较时是:if(abs((float_variable - value)

标签: c


【解决方案1】:

您可以使用无限循环并打破它,以防万一gallons_used = -1

for(;;) { // <-- infinite loop
    printf("Enter the gallons used (-1 to end): ");
    scanf("%f", &gallons_used);
    if (gallons_used == -1)
        break; // <-- exit the loop
    printf("Enter the miles driven: ");
    scanf("%f", &miles);
    mpg = miles / gallons_used;
    printf("Miles / gallon for this tank was %f\n", mpg);
    total += mpg;
    count++;
}

【讨论】:

  • 这也不起作用,它和以前一样,仍然要求最后一个输入。
  • @user3765259:要求输入,但如果输入为-1则退出,因此它应该可以工作
【解决方案2】:
while(true) {
printf("Enter the gallons used (-1 to end): ");
    scanf("%f", &gallons_used);
    printf("Enter the miles driven: ");
    scanf("%f", &miles);
    if(gallons_used== -1 )break;
    mpg = miles / gallons_used;
    printf("Miles / gallon for this tank was %f\n", mpg);
    total += mpg;
    count++;
}

【讨论】:

  • timmy 的回答更好,如果您输入 -1 表示加仑,它不会询问里程:)
【解决方案3】:
#include <stdio.h>

int main(void) {
    int total = 0, count = 0;
    float gallons_used, mpg, miles;
    while(gallons_used != -1) {
    printf("Enter the gallons used (-1 to end): ");
        scanf("%f", &gallons_used);
        if (gallons_used < 0)             // check gallons_used
            break;
        printf("Enter the miles driven: ");
        scanf("%f", &miles);
        mpg = miles / gallons_used;
        printf("Miles / gallon for this tank was %f\n", mpg);
        total += mpg;
        count++;
    }
    total /= count;
    printf("Average miles to the gallon was: %d\n", total);
    return 0;
}

【讨论】:

    【解决方案4】:

    您正在使用未初始化的gallons_used。使用未初始化的变量会调用未定义的行为。你需要先初始化它,然后在while的条件表达式中进行比较。你可以这样做

    printf("Enter the gallons used (-1 to end): ");
    scanf("%f", &gallons_used);           // Reading value for gallons_used
    
    while(gallons_used != -1) {
        printf("Enter the miles driven: ");
        scanf("%f", &miles);
        mpg = miles / gallons_used;
        printf("Miles / gallon for this tank was %f\n", mpg);
        total += mpg;
        count++;
        printf("Enter the gallons used (-1 to end): ");
        scanf("%f", &gallons_used);
    }  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      相关资源
      最近更新 更多