在C

时间:2018-11-10 11:43:29

标签: c variables if-statement declaration

我是C语言的新手,而不是整体编程。我想知道为什么当我声明要在结构的if语句外使用的变量时,我收到的输出是错误的(无论如何对于这段代码)。

这是我的代码:

#include<stdio.h>
void grossPay();

int main()
{
    grossPay();
}

void grossPay()
{
int rate = 10, hours;
double tax, grosspay, netpay;

printf("Enter work hours this week: ");
scanf("%d", &hours);

grosspay = hours * rate;

if (grosspay <= 300 && grosspay > 0)
{
    tax = 0.10;
    netpay = grosspay - grosspay * tax;
    printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
    printf("Gross pay: $%.2f\n", grosspay);
    printf("Net pay: $%.2f\n", netpay);
}
else if (grosspay > 300 && grosspay <=1000)
{
    tax = 0.15;
    netpay = grosspay - grosspay * tax;
    printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
    printf("Gross pay: $%.2f\n", grosspay);
    printf("Net pay: $%.2f\n", netpay);
}
else if (grosspay > 1000)
{
    tax = 0.25;
    netpay = grosspay - grosspay * tax;
    printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
    printf("Gross pay: $%.2f\n", grosspay);
    printf("Net pay: $%.2f\n", netpay);
}
else 
{
    printf("Invalid input. Please try again.\n\n");
}
}

编辑:我放置的代码是我的“修复”,用于未获得正确的输出。我希望当我在整个IF语句外声明一次netpay变量时,我会收到正确的输出,与上面代码中的输出相同。

编辑2:以前的版本

#include<stdio.h>
void grossPay();

int main()
{
    grossPay();
}

void grossPay()
{
    int rate = 10, hours;
    double tax, grosspay, netpay;

    printf("Enter work hours this week: ");
    scanf("%d", &hours);

    grosspay = hours * rate;
    netpay = grosspay - grosspay * tax;

    if (grosspay <= 300 && grosspay > 0)
    {
        tax = 0.10;

        printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
        printf("Gross pay: $%.2f\n", grosspay);
        printf("Net pay: $%.2f\n", netpay);
    }
    else if (grosspay > 300 && grosspay <=1000)
    {
        tax = 0.15;
        printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
        printf("Gross pay: $%.2f\n", grosspay);
        printf("Net pay: $%.2f\n", netpay);
    }
    else if (grosspay > 1000)
    {
        tax = 0.25;
        printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
        printf("Gross pay: $%.2f\n", grosspay);
        printf("Net pay: $%.2f\n", netpay);
    }
    else 
    {
        printf("Invalid input. Please try again.\n\n");
    }
}

2 个答案:

答案 0 :(得分:1)

相关代码归结为:

double tax;
netpay = grosspay - grosspay * tax;
tax = 0.10;
printf("Net pay: $%.2f\n", netpay);

问题是程序中的语句按照您编写语句的顺序执行(至少在函数中,除非使用特殊的控制流语句,例如continuegoto)。

因此:

  1. 首先,我们定义一个名为tax的局部变量,该变量未初始化。
  2. 然后,我们将netpay设置为grosspay - grosspay * tax的结果。这已经是错误的,因为tax目前没有定义的值,因此grosspay - grosspay * tax会产生未定义的结果。
  3. 然后我们设置tax。这对netpay的值没有影响。
  4. 然后我们打印netpay

事情以错误的顺序发生。您需要先设置变量,然后再使用它们。

这就像您在告诉某人:

  1. 阅读您手中的书。
  2. 指环王
  3. 打开它。

您想知道为什么他们不读《指环王》。

答案 1 :(得分:0)

也许您正在尝试消除重复的代码。由于if块之间的差异是税率,因此您可以设置税率,最后进行一次计算。

#include<stdio.h>
void grossPay();

int main()
{
    grossPay();
}

void grossPay()
{
    int rate = 10, hours;
    double tax, grosspay, netpay;

    printf("Enter work hours this week: ");
    scanf("%d", &hours);

    grosspay = hours * rate;

    if (grosspay <= 300 && grosspay > 0)
    {
        tax = 0.10;
    }
    else if (grosspay > 300 && grosspay <=1000)
    {
        tax = 0.15;
    }
    else if (grosspay > 1000)
    {
        tax = 0.25;
    }
    else
    {
        printf("Invalid input. Please try again.\n\n");
        return;
    }
    netpay = grosspay - grosspay * tax;
    printf("Pay for %d hours of week with $%d per hour\n", hours, rate);
    printf("Gross pay: $%.2f\n", grosspay);
    printf("Net pay: $%.2f\n", netpay);
}