我是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");
}
}
答案 0 :(得分:1)
相关代码归结为:
double tax;
netpay = grosspay - grosspay * tax;
tax = 0.10;
printf("Net pay: $%.2f\n", netpay);
问题是程序中的语句按照您编写语句的顺序执行(至少在函数中,除非使用特殊的控制流语句,例如continue
或goto
)。
因此:
tax
的局部变量,该变量未初始化。netpay
设置为grosspay - grosspay * tax
的结果。这已经是错误的,因为tax
目前没有定义的值,因此grosspay - grosspay * tax
会产生未定义的结果。tax
。这对netpay
的值没有影响。netpay
。事情以错误的顺序发生。您需要先设置变量,然后再使用它们。
这就像您在告诉某人:
您想知道为什么他们不读《指环王》。
答案 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);
}