我想知道如何摆脱运行时出现的1.#J输出。我知道它与行程选择部分之后的float mpg变量有关。我刚刚开始使用C,因此非常感谢您的帮助。另外,程序尚未完成。

#include <stdio.h>
#define MAX_LEN 80

float mpg, distance;

int main ( )
{
    int name[MAX_LEN];
    printf("Hello! Can you please enter your name: ");
    scanf("%s", name);
    printf("Okay, %s, Let's begin!\n", name);

    char answer;
    printf("%s, do you own a car? (Y or N):");
    scanf(" %c", &answer);
    while (answer == 'y' || answer == 'Y')
    {
        printf("Great, welcome to the road trip calculator.\n");
        printf("\n");
        printf("First we will need to know somethings about your car.\n");

        float gal_to_fill;

        printf("About how many gallons of gas does it take to fill it up: ");
        scanf("%f", &gal_to_fill);
        printf("%.2f\n",gal_to_fill);

        float mpg;
        printf("About how many miles can you drive per gallon: ");
        scanf("%f", &mpg);
        printf("%.2f\n",mpg);

        printf("Okay, now let's pick a location to travel to.\n");
        printf("We are going to start at ________\n");
        printf("Please pick a final destination:\n");
        printf("1. Boston, MA\n");
        printf("2. New York, NY\n");
        printf("3. Miami, FL\n");
        printf("4. Chicago, IL\n");
        printf("5. San Francisco, CA\n");
        break;
    }

    int trip;
    printf("Enter the number of your choice: ");
    scanf("%d", &trip);

    while(trip == 1,2,3,4,5)
    {
        if (trip == 1)
        {
            float mpg;
            float distance = 141.9;
            printf("This trip is 141.9 miles\n");
            float gal_used;
            gal_used = distance/mpg;
            printf("%.2f\n",gal_used);
            break;
        }
        else if (trip == 2)
        {
            float distance = 343.6;
            printf("This trip is 343.6 miles\n");
            float gal_used;
            gal_used = distance/mpg;
            printf("%.2f\n", gal_used);
            break;
        }
        else if (trip == 3)
        {
            float distance = 1623.3;
            printf("This trip is 1623.3 miles\n");
            float gal_used;
            gal_used = distance/mpg;
            printf("%.2f\n", gal_used);
            break;
        }
        else if (trip == 4)
        {
            float distance = 1112.8;
            printf("This trip is 1112.8 miles\n");
            float gal_used;
            gal_used = distance/mpg;
            printf("%.2f\n", gal_used);
            break;
        }
        else if (trip == 5)
        {
            float distance = 3227.2;
            printf("This trip is 3227.2 miles\n");
            float gal_used;
            gal_used = distance/mpg;
            printf("%.2f\n", gal_used);
            break;
        }
    }
    return 0;
}

最佳答案

代码有太多问题。为什么要声明这么多float mpg? while循环中的mpg将隐藏全局版本。另外在行程选择部分:

float mpg;
float distance = 141.9;
printf("This trip is 141.9 miles\n");
float gal_used;
gal_used = distance/mpg;


您正在使用mpg且未初始化且会调用未定义的行为。 mpg中的垃圾可以返回任何内容,包括1.#J。与其他if块中的其他mpg变量相同,因为它是尚未初始化的全局变量。

另一个问题:

while(trip == 1,2,3,4,5)


那不会按照您的想法做,程序将永远循环。首先阅读how the comma operator work。将循环更改为while (trip >= 1 && trip <= 5)

10-07 13:41