C语言(如果...否则)

C语言(如果...否则)

问题描述:

#include <stdio.h>
void main (void)
{
    float no_week =0.0;
    int no_hour;
    float hour_rate;
    int no_item;
    int salary_perweek;
    float paycode_1;
    float paycode_2;
    float paycode_21;
    float paycode_22;
    float paycode_3;
    float paycode_4;
    float salarypaycode_1;
    float salarypaycode_21 = 0.0;
    float salarypaycode_22 = 0.0;
    salary_perweek = 500;
    hour_rate = 2.5;
    printf("Enter Employee pay code: ");
    scanf("%i", &paycode_1);
    printf("Enter Number of Week: ");
    scanf("%g", &no_week);
    salarypaycode_1 = no_week * salary_perweek;
    printf("Manager''s Salary is $%g\n", salarypaycode_1);
    printf("Enter Employee pay code: ");
    scanf("%i", &paycode_2);
    printf("Enter Number of Hour: ");
    scanf("%g", &no_hour);
    while (no_hour >=0)
    {
        if (no_hour<=40)
        salarypaycode_21 = no_hour * hour_rate;
                {   printf("Hourly Workers Salary is %g\n", salarypaycode_21);
        }
        else
        if (no_hour>40)
        salarypaycode_22 = no_hour * hour_rate;
        {   printf("Hourly Workers Salary is %g\n", salarypaycode_22);
        }
    }

}




我的编码是找到员工的薪水.所以变量不会打扰它第一,因为我还没有完成我的编码.它只是它的一部分,我坚持了这一部分.该公式无法在if .... else中运行,也无法获得结果.我可以知道错误在哪里吗?我无法解决.




My coding is find the salary pay for the employee. So the variable dont bother it 1st because i haven finished my coding. It just part on it, and i stuck on this part.The formula cant run in the if....else and cant get the result. May i know where is the error? i cant figure it out.

%g \ n,salarypaycode_1); printf("Enter Employee pay code:"); scanf(%i",& paycode_2); printf("Enter Hour of Hour:"); scanf(%g",& no_hour); 一会儿(no_hour> = 0) { 如果(no_hour< = 40) Salarypaycode_21 = no_hour * hour_rate; {printf(每小时工薪是%g \ n",salarypaycode_21); } 别的 如果(no_hour> 40) Salarypaycode_22 = no_hour * hour_rate; {printf(每小时工人工资为%g \ n",salarypaycode_22); } } }
%g\n", salarypaycode_1); printf("Enter Employee pay code: "); scanf("%i", &paycode_2); printf("Enter Number of Hour: "); scanf("%g", &no_hour); while (no_hour >=0) { if (no_hour<=40) salarypaycode_21 = no_hour * hour_rate; { printf("Hourly Workers Salary is %g\n", salarypaycode_21); } else if (no_hour>40) salarypaycode_22 = no_hour * hour_rate; { printf("Hourly Workers Salary is %g\n", salarypaycode_22); } } }




我的编码是找到员工的薪水.所以变量不会打扰它第一,因为我还没有完成我的编码.它只是它的一部分,我坚持了这一部分.该公式无法在if .... else中运行,也无法获得结果.我可以知道错误在哪里吗?我无法弄清楚.




My coding is find the salary pay for the employee. So the variable dont bother it 1st because i haven finished my coding. It just part on it, and i stuck on this part.The formula cant run in the if....else and cant get the result. May i know where is the error? i cant figure it out.


您将大括号放在if else语句周围.我认为这样做会更好.

You have misplaced the braces around your if else statement. I think this would work better.

if (no_hour<=40)
{
   salarypaycode_21 = no_hour * hour_rate;
    printf("Hourly Workers Salary is %g\n", salarypaycode_21);
}
else ...


括号是个小东西,很容易找到; Visual Studio将显示匹配的括号(您看到了吗)…

但是看看你在做什么!

Bracket is a small thing, easy to find; Visual Studio will show the matching brackets (did you see it)…

But look at what are you doing!

if (no_hour<=40)
{
}
else
if (no_hour>40) //this line is redundant! When code runs at this point it's always true!
{
}



如果一个条件与另一个条件相反,为什么要使用两个条件.这不是一个小错误,这是错误的思维方式.现在,假设您将40替换为42(相信我,没有常数会长期保持不变).您可以仅在一种情况下轻松更改它并破坏代码.因此,永远都没有立即常数! (您的代码中的立即数40被称为立即常量,在编译和JIT之后直接进入代码.)

将所有常量放在显式声明为常量的位置.
在其他情况下,它们不应为常量,而应为变量,资源中的对象或某些配置文件.
永远不要假设任何常量永远具有相同的值.


最后的循环永远不会结束.怎么会这样如果(no_hour >=0),则代码将进入循环,因为这是一个循环条件,因为条件保持为真,因为它以后永远不会修改,所以它会永远保留在循环中.

—SA



Why using two conditions if one is the opposite to another. And this is not a small mistake, this is wrong way of thinking. Now, imagine you replace 40 with 42 (believe me, no constant stay a constant for long). You can easily change it only in one case and break your code. So, no immediate constants, ever! (The literal 40 in your code is called immediate constant, it goes right into the code after compilation and JIT.)

Keep all constants in the same place declared as constants explicitly.
In other cases, they should not be constants but variables, objects in resources or some configuration files.
Never assume any constant to be of the same value forever.


The loop at the end is never finished. How can it? If (no_hour >=0) the code will get into the loop, because this is a loop condition, it remains in the loop forever, because condition remains true, because no_hour value is never modified thereafter.

—SA