Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
if语句成功后如何停止?C程序设计_C_If Statement - Fatal编程技术网

if语句成功后如何停止?C程序设计

if语句成功后如何停止?C程序设计,c,if-statement,C,If Statement,我正在尝试获取用户输入,以找出从工资中扣除的金额,因此他们将自己的ID和工资放在ID号的范围内。例如,如果我输入ID 1,然后输入500的工资,它应该输出输入的工资、扣除的工资和净工资。它可以完成所有这些工作,但如果它检查工资是否在正确的ID号范围内,它会在检查输入的ID后使用每个ID号进行检查 #include <stdio.h> #include <stdlib.h> int main() { int ID; // variable for ID

我正在尝试获取用户输入,以找出从工资中扣除的金额,因此他们将自己的ID和工资放在ID号的范围内。例如,如果我输入ID 1,然后输入500的工资,它应该输出输入的工资、扣除的工资和净工资。它可以完成所有这些工作,但如果它检查工资是否在正确的ID号范围内,它会在检查输入的ID后使用每个ID号进行检查

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int ID;  // variable for ID
    float Base_Salary; // variable for base salary
    float Amount_Deducted; // variable for amount deducted from employee
    float Net_Salary; // variable for employees' net salary

    printf("%s", "Enter ID: \n"); // prompt for ID number
    scanf("%d", &ID );

    // Validate user's ID
    if (ID == 1)
        printf("You have entered the ID 1\n");
    else { if (ID == 2)
            printf("You have entered the ID 2\n");
            else { if (ID == 3)
                printf("You have entered the ID 3\n");
                    else { if (ID == 4)
                        printf("You have entered the ID 4\n");
                            else { if (ID == 5)
                                printf("You have entered the ID 5\n");
                                    else { if (ID < 1 && ID > 5)
                                        printf("You didn't enter a proper ID\n");
                                    }
                            }
                    }
            }
    }

    printf("%s", "Enter your salary in the appropriate range: \n");
    scanf("%f",&Base_Salary);

    // Validations

       if (ID == 1 && Base_Salary >= 100 && Base_Salary <= 1000) {
            printf("Base salary is in range for ID given.\n");
       }
       else {
            printf("Salary must be between 100-1000 for ID 1.\n");
       }

       if (ID == 2 && Base_Salary >= 1001 && Base_Salary <= 5000) {
            printf("Base salary is in range for ID given.\n");
       }
       else {
            printf("Salary must be between 1001-5000 for ID 2.\n");
       }

       if (ID == 3 && Base_Salary >= 5001 && Base_Salary <= 10,000) {
            printf("Base salary is in range for ID given.\n");
       }
       else {
            printf("Salary must be between 5001-10,000 for ID 3.\n");
       }

       if (ID == 4 && Base_Salary >= 10,001 && Base_Salary <= 15,000) {
            printf("Base salary is in range for ID given.\n");
       }
       else {
            printf("Salary must be between 10,001-15,000 for ID 4.\n");
       }

       if (ID == 5 && Base_Salary >= 15,001 && Base_Salary <= 20,000) {
            printf("Base salary is in range for ID given.\n");
       }
       else {
            printf("Salary must be between 15,001-20,000 for ID 5.\n");
       }

    // Calculations
    if (ID == 1) {
        Amount_Deducted = Base_Salary * 0.50;
        Net_Salary = Base_Salary - Amount_Deducted;
        printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
        printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
        printf("The Net Salary is = %.2f.\n", Net_Salary);
    }
    if (ID == 2) {
        Amount_Deducted = Base_Salary * 1.50;
        Net_Salary = Base_Salary - Amount_Deducted;
        printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
        printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
        printf("The Net Salary is = %.2f.\n", Net_Salary);
    }

    if (ID == 3) {
        Amount_Deducted = Base_Salary * 2.50;
        Net_Salary = Base_Salary - Amount_Deducted;
        printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
        printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
        printf("The Net Salary is = %.2f.\n", Net_Salary);
    }

    if (ID == 4) {
        Amount_Deducted = Base_Salary * 3.50;
        Net_Salary = Base_Salary - Amount_Deducted;
        printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
        printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
        printf("The Net Salary is = %.2f.\n", Net_Salary);
    }

    if (ID == 5) {
        Amount_Deducted = Base_Salary * 4.50;
        Net_Salary = Base_Salary - Amount_Deducted;
        printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
        printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
        printf("The Net Salary is = %.2f.\n", Net_Salary);
    }

}
#包括
#包括
int main()
{
int ID;//ID的变量
浮动基本工资;//基本工资的变量
浮动金额\u扣除;//从员工扣除的金额的变量
浮动净工资;//员工净工资的变量
printf(“%s”,“输入ID:\n”);//提示输入ID号
scanf(“%d”和&ID);
//验证用户的ID
如果(ID==1)
printf(“您已输入ID 1\n”);
else{if(ID==2)
printf(“您已输入ID 2\n”);
else{if(ID==3)
printf(“您已输入ID 3\n”);
else{if(ID==4)
printf(“您已输入ID 4\n”);
else{if(ID==5)
printf(“您已输入ID 5\n”);
else{if(ID<1&&ID>5)
printf(“您没有输入正确的ID\n”);
}
}
}
}
}
printf(“%s”,“在适当的范围内输入您的工资:\n”);
scanf(“%f”和基本工资);
//验证

如果(ID==1&&Base\u-Salary>=100&&Base\u-Salary=1001&&Base\u-Salary=5001&&Base\u-Salary=10001&&Base\u-Salary=15001&&Base\u-Salary,看起来开关盒
比“如果其他”更合适:

// Validate user's ID
switch (ID)
{
case 1: 
    printf("You have entered the ID 1\n");
    if (Base_Salary >= 100 && Base_Salary <= 1000)
    {
        printf("Base salary is in range for ID given.\n");
    }
    Amount_Deducted = Base_Salary * 0.50;
    Net_Salary = Base_Salary - Amount_Deducted;
    break;

case 2:
    printf("You have entered the ID 2\n");
    /* Base Salary & Amount Deducted, code here. */
    break;

case 3:
    printf("You have entered the ID 3\n");
    /* Base Salary & Amount Deducted, code here. */
    break;

case 4:
    printf("You have entered the ID 4\n");
    /* Base Salary & Amount Deducted, code here. */
    break;

case 5:
    printf("You have entered the ID 5\n");
    /* Base Salary & Amount Deducted, code here. */
    break;

default:
    printf("You didn't enter a proper ID\n");
    break;
}
//验证用户的ID
开关(ID)
{
案例1:
printf(“您已输入ID 1\n”);
如果(基本工资>=100&&基本工资逻辑显示“如果ID==1&&..检查工资范围…否则打印错误”,这意味着对于每个不是1的ID,您将打印错误

我想你想改变这一点:

if (ID == 1 && Base_Salary >= 100 && Base_Salary <= 1000) {
    printf("Base salary is in range for ID given.\n");
}
else {
    printf("Salary must be between 100-1000 for ID 1.\n");
}

如果(ID==1&&Base\u Salary>=100&&Base\u Salary=100&&Base\u Salary要解决您的确切问题,请使用elseif

如果If语句为true,则跳过其下的所有其他elseif

if (ID == 1) {
    Amount_Deducted = Base_Salary * 0.50;
    Net_Salary = Base_Salary - Amount_Deducted;
    printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
    printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
    printf("The Net Salary is = %.2f.\n", Net_Salary);
}

else if (ID == 2) {
        Amount_Deducted = Base_Salary * 1.50;
        Net_Salary = Base_Salary - Amount_Deducted;
        printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
        printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
        printf("The Net Salary is = %.2f.\n", Net_Salary);
    }

else if (ID == 3) {
    Amount_Deducted = Base_Salary * 2.50;
    Net_Salary = Base_Salary - Amount_Deducted;
    printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
    printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
    printf("The Net Salary is = %.2f.\n", Net_Salary);
}

else if (ID == 4) {
    Amount_Deducted = Base_Salary * 3.50;
    Net_Salary = Base_Salary - Amount_Deducted;
    printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
    printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
    printf("The Net Salary is = %.2f.\n", Net_Salary);
}

else if (ID == 5) {
    Amount_Deducted = Base_Salary * 4.50;
    Net_Salary = Base_Salary - Amount_Deducted;
    printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
    printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
    printf("The Net Salary is = %.2f.\n", Net_Salary);
}

一种更干净的方法是使用数组并使用索引在其上循环,因为您的代码在每个IF块中都是冗余的。您可以轻松地将其重构为一个方法,或者将其写入FOR循环中,以最大限度地提高代码重用率并减少代码重复。

从原始注释继续,您的编译器应该会告诉您您的位置r初始错误存在。如果未启用警告,则养成编译时至少启用
-Wall-Wextra
的习惯,然后读取并不接受使用任何警告编译的代码。(您不太可能遇到无法消除警告的任何合法情况)如果您选择,还可以添加
-pedantic
以获取其他警告

你应该告诉你,例如

注意:您扣除的
金额不太可能是通过乘以大于
1的值来计算的,否则您的
净工资将为负值

最后,虽然有许多方法可以验证范围和扣除率,但在类似的情况下,使用数组创建的简单查找表提供了非常有效的解决方案,而不是大量的
if…else…
switch
语句。例如:

#include <stdio.h>

#define IDMAX 5

int main (void)   /* affirmatively indicate no arguments expected */
{
    int ID = 0,   /* initialize variables */
        range [][2] = {{   100,  1000}, /* salary range lookup */
                       {  1001,  5000},
                       {  5001, 10000},
                       { 10001, 15000},
                       { 15001, 20000}};
    float Base_Salary = 0.0,
        Amount_Deducted = 0.0,
        Net_Salary = 0.0,
        rate[] = { .05, .15, .25, .35, .45 };  /* rate lookup */

    printf ("Enter ID: ");  /* prompt for ID number & validate */
    if (scanf ("%d", &ID) != 1 || ID < 1 || ID > IDMAX) {
        fprintf (stderr, "error: invalid ID\n");
        return 1;
    }

    printf ("Enter your salary in the appropriate range: ");
    if (scanf ("%f", &Base_Salary) != 1) { /* validate entry */
        fprintf (stderr, "error: invalid Base_Salary\n");
        return 1;
    }

    /* validate Base_Salary in range */
    if (Base_Salary < range[ID-1][0] || Base_Salary > range[ID-1][1]) {
        fprintf (stderr, "error: Salary must be between %d-%d for ID %d.\n",
                range[ID-1][0], range[ID-1][1], ID);
        return 1;
    }
    else
        printf ("Base salary is in range for ID given.\n");

    Amount_Deducted = Base_Salary * rate[ID-1]; /* calculations */
    Net_Salary = Base_Salary - Amount_Deducted;

    printf ("The Base Salary you have entered = %.2f.\n", Base_Salary);
    printf ("The Amount of salary deducted is = %.2f.\n",
            Amount_Deducted);
    printf ("The Net Salary is = %.2f.\n", Net_Salary);

    return 0;   /* main is type 'int' and returns a value */
}
始终在启用警告的情况下编译,并且在没有警告的情况下编译之前不要删除任何代码,这样您的调试时间将大大减少。代码注释中还包含一些其他指针。如果您有任何问题,请告诉我


最后一点注意:虽然不是一个错误,但C的标准样式通常避免了
大小写混合
caMelCase
变量名,而支持所有小写,并为宏和预处理器定义保留大写。您的变量名保留原样,以帮助您跟进。这是一个错误,但值得一提ing意识到。

switch语句怎么样?这是一个很好的台阶。摆脱它!行
55、61、67
-“警告:逗号表达式的左操作数无效”-您没有阅读编译器警告,是吗?或者
else if{…}
方法可能
if(ID>=1&&ID是的,你是对的。我错过了,并且编辑了答案。
salary.c:55:60: warning: left-hand operand of comma expression has no effect
[-Wunused-value]
     if (ID == 3 && Base_Salary >= 5001 && Base_Salary <= 10, 000) {
                                                            ^
printf ("Enter ID: ");  /* prompt for ID number & validate */
if (scanf ("%d", &ID) != 1 || ID < 1 || ID > IDMAX) {
    fprintf (stderr, "error: invalid ID\n");
    return 1;
}
#include <stdio.h>

#define IDMAX 5

int main (void)   /* affirmatively indicate no arguments expected */
{
    int ID = 0,   /* initialize variables */
        range [][2] = {{   100,  1000}, /* salary range lookup */
                       {  1001,  5000},
                       {  5001, 10000},
                       { 10001, 15000},
                       { 15001, 20000}};
    float Base_Salary = 0.0,
        Amount_Deducted = 0.0,
        Net_Salary = 0.0,
        rate[] = { .05, .15, .25, .35, .45 };  /* rate lookup */

    printf ("Enter ID: ");  /* prompt for ID number & validate */
    if (scanf ("%d", &ID) != 1 || ID < 1 || ID > IDMAX) {
        fprintf (stderr, "error: invalid ID\n");
        return 1;
    }

    printf ("Enter your salary in the appropriate range: ");
    if (scanf ("%f", &Base_Salary) != 1) { /* validate entry */
        fprintf (stderr, "error: invalid Base_Salary\n");
        return 1;
    }

    /* validate Base_Salary in range */
    if (Base_Salary < range[ID-1][0] || Base_Salary > range[ID-1][1]) {
        fprintf (stderr, "error: Salary must be between %d-%d for ID %d.\n",
                range[ID-1][0], range[ID-1][1], ID);
        return 1;
    }
    else
        printf ("Base salary is in range for ID given.\n");

    Amount_Deducted = Base_Salary * rate[ID-1]; /* calculations */
    Net_Salary = Base_Salary - Amount_Deducted;

    printf ("The Base Salary you have entered = %.2f.\n", Base_Salary);
    printf ("The Amount of salary deducted is = %.2f.\n",
            Amount_Deducted);
    printf ("The Net Salary is = %.2f.\n", Net_Salary);

    return 0;   /* main is type 'int' and returns a value */
}
$ ./bin/salary
Enter ID: 1
Enter your salary in the appropriate range: 99
error: Salary must be between 100-1000 for ID 1.

$ ./bin/salary
Enter ID: 1
Enter your salary in the appropriate range: 1000
Base salary is in range for ID given.
The Base Salary you have entered = 1000.00.
The Amount of salary deducted is = 50.00.
The Net Salary is = 950.00.

$ ./bin/salary
Enter ID: 2
Enter your salary in the appropriate range: 1000
error: Salary must be between 1001-5000 for ID 2.