【问题标题】:How do I fix my if statement not being recognized as true如何修复我的 if 语句未被识别为真
【发布时间】:2020-02-08 10:10:59
【问题描述】:

我编写了我的程序,该程序应该检查某人是否单身、已婚(分居或共同)以及他们是否是一家之主,以计算他们必须支付的税款。由于某种原因,当我为第一个 scanf 键入 y 时,下一个 if 语句被跳过。请帮忙!这是我的整个代码。我知道它不是最优化的,我可以清理它,但我只是想先弄清楚它有什么问题。

    void question3 (void)
    {
    char single, head, joint;
    int income = 0;
    double tax = 0;

    printf("What is your total income?");
    scanf("%d", &income);

    printf ("Are you single? (y/n) \n");
    scanf (" %c", &single);

    if (single == 'y')
    {
        if (income == 17850)
        {
            tax = income * .15;
        }
        else if (income >= 17850)
        {
            tax = ((.15 * 17850) + (.28 * (income - 17850)));
        }
    }
    else
    {
        if (single != 'y')
        {
        printf("Are you the head of the house hold? y or n\n");
        scanf(" %c", &head);
        }
    }

    if (head == 'y')
    {
        if (income == 23900)
        {
         tax = income * .15;
        }
        else if (income >= 23900)
        {
         tax = ((.15 * 23900) + (.28 * (income - 23900)));
        }
    }
    else if (head != 'y')
    {
        printf("Do you have a joint marriage\n");
        scanf(" %c", &joint);
    }

    if (joint == 'y')
    {
        if (income == 29750)
        {
         tax = income * .15;
        }
        else if (income >= 29750)
        {
        tax = ((.15 * 29750) + (.28 * (income - 29750)));
        }
    }
    else if (joint != 'y')
    {
        if(income == 14875)
        {
         tax = income * .15;
        }
        else if (income >= 14875)
        {
         tax = ((.15 * 14875) + (.28 * (income - 14875)));
        }

    }

    printf("You owe %f dollars in taxes.", tax);
    }

【问题讨论】:

  • 第一次打电话给scanf不是要收入吗?此外,您的 if 语句可能应该检查收入是否小于一个金额,而不是完全等于它。
  • 当一个简单的else 可以做到时,你的else if (blah != 'y') 是没有理由的。此外,如果income 小于您的幻数(如17850)会怎样?您应该拥有if <else。手动遍历您的逻辑,并思考每个值会发生什么。
  • Joint 和最后一个 else if 的代码也可以正常工作并给出正确的结果(分别为 7332.5 和 9266.5),但是当我输入 y 作为 single 时,它​​会继续问我家庭问题以及是否我对那个说 y 然后它给了我最后一个 else if 的答案。我只是不知道如何解决它。
  • 您应该以char head = 'y'; 开头,否则,当single'y' 时,没有为head 分配值。顺便说一句,编译器应该警告你。

标签: c if-statement boolean scanf


【解决方案1】:

您可以使用函数将其分解,这样对于每个真正的表达式,它都会调用一个函数。在每个功能中,您都要求收入,然后给出结果。打印结果后,它会调用 question3 来重复循环。

对于每一个错误的表达式,你都会给出另一种选择,它要么调用一个函数,要么给出另一种选择。

char single, head, joint;
int income = 0;
double tax = 0;

void Single();
void Head();
void Joint();

void question3(void)
{
    printf("Are you single? (y/n) \n");
    scanf(" %c", &single);

    if (single == 'y')
    {
        Single();
    }
    else if (single == 'n')
    {
        printf("Are you the head of the house hold? y or n\n");
        scanf(" %c", &head);

        if (head == 'y')
        {
            Head();
        }
        else if (head == 'n')
        {
            printf("Do you have a joint marriage\n");
            scanf(" %c", &joint);

            if (joint == 'y')
            {
                Joint();
            }
            else if(joint == 'n')
            {
               question3();
            }
        }
    }
}

void Single()
{
    printf("What is your total income?");
    scanf("%d", &income);

    if (income == 17850)
    {
        tax = income * .15;
    }
    else if (income >= 17850)
    {
        tax = ((.15 * 17850) + (.28 * (income - 17850)));
    }

    printf("You owe %f dollars in taxes.\n\n", tax);

    question3();

    return 0;
}

void Head()
{
    printf("What is your total income?");
    scanf("%d", &income);

    if (income == 23900)
    {
        tax = income * .15;
    }
    else if (income >= 23900)
    {
        tax = ((.15 * 23900) + (.28 * (income - 23900)));
    }

    printf("You owe %f dollars in taxes.\n\n", tax);

    question3();

    return 0;
}

void Joint()
{
    printf("What is your total income?");
    scanf("%d", &income);

    if (income == 29750)
    {
        tax = income * .15;
    }
    else if (income >= 29750)
    {
        tax = ((.15 * 29750) + (.28 * (income - 29750)));
    }

    printf("You owe %f dollars in taxes.\n", tax);

    question3();

    return 0;
}

void main()
{
   question3();

return 0;
}

【讨论】:

    【解决方案2】:

    对于单个字符,您应该使用getchar() 而不是scanf

    single = getchar();
    

    如果你坚持scanf:

    • 您应该删除空格(因此scanf("%c", &single))。
    • 您可能应该测试scanf 的返回:它返回它可以解析的 arg 数量(在您的情况下,如果可以,则应该为 1,否则为 0)。

    另一方面,这是没用的:

    if (single == 'y') {
      ...
    } else if (single != 'y') {
      ...
    }
    

    如果你不关心单个存在绝对等于 y 或 n,那么你应该像这样写:

    if (single == 'y') {
      ...
    } else {
      ...
    }
    

    【讨论】:

    • 不是我的 DV,但” %c” 中的空格是正确且重要的。
    • 我用 C 编码已经很久了。但我不记得使用“%c”(或者我会使用 getchar())。
    • 我不确定当你使用 C 时它是否是广泛的知识。这是我在过去十年里学到的东西——自从加入 SO 以来,但很久以前——但它处理前一个输入(收入)留下的换行符。即使是现在,我也大多避免使用scanf()fscanf();我优先使用fgets()sscanf() 或类似的东西。
    猜你喜欢
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多