0

我在输入值 60000.01、70000.01、75000.01 和 88000.01 时遇到嵌套 if 语句的问题,RaisePrcnt 值不会打印到控制台。我看不出嵌套 if 的结构存在缺陷,除了这些特定值之外的任何其他值都可以正常工作。

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

void main()
{
    // Employee and Department name variable declarations.
    char Name1[25], Dept1[25];  

    /* Variable declarations for current yearly income, raise percentage, 
    new raise amount and the new yearly pay amount.*/
    float Income1, NewPay1;
    float RaisePrcnt1 = 9.7, RaisePrcnt2 = 8.5, RaisePrcnt3 = 7.2;
    float RaisePrcnt4 = 6.3, RaisePrcnt5 = 5.9, Percentage = 0;

    /*Initialize and declare variables to calculate the total current
    yearly incomes, total raise amounts and total new pay amounts for
    all 4 employees.*/
    float IncomeTotal = 0;
    float RaiseTotal = 0; 
    float PayTotal = 0;

    // Display program title.
    printf("\t\t\t\tINCOME CALCULATOR\n\n"); 

    // User directive.
    printf("\t\t************************************************\n");
    printf("\t\tPLEASE ENTER THE FOLLOWING EMPLOYEE INFORMATION: "); 
    printf("\n\t\t************************************************\n\n");

    /************************************************************
    BEGIN COLLECTING INFORMATION ON EMPLOYEE NUMBER 1.          *
    ************************************************************/
    printf("\t\tEmployee Number 1: \n");
    printf("\t\t________________________________________________\n");

    printf("\n\t\tName: \t\t\t"); // Prompt user for Name.
    gets(Name1); // Reads input for Name to the enter key.

    printf("\n\t\tDepartment: \t\t"); // Prompt user for Department Name.
    gets(Dept1); // Reads Department Name input to the enter key.

    // Prompt user for Current Yearly Income input.
    printf("\n\t\tCurrent Yearly Income: \t"); 
    scanf("%f", &Income1); // Reads Current Income input.

    // Prompt user for the Raise Percentage.
    //printf("\n\t\tRaise Percentage: \t");
    //scanf("%f", &RaisePercent1); // Reads Raise Percentage input.


    if(Income1 < 0 && Income1 != 0){
       printf("%0.1f The Income Amount entered is INVALID. \t");
    }else
        if(Income1 >= 0 && Income1 <= 60000){
           printf("%0.1f", RaisePrcnt1);
        }else
           if(Income1 >= 60000.01 && Income1 <= 70000){
              printf("%0.1f", RaisePrcnt2);
           }else
              if(Income1 >= 70000.01 && Income1 <= 75000){
                 printf("%0.1f", RaisePrcnt3);
              }else
                 if(Income1 >= 75000.01 && Income1 <= 88000){
                    printf("%0.1f", RaisePrcnt4);
                 }else
                    if(Income1 >= 88000.01){
                    printf("%0.1f", RaisePrcnt5);
                    }



 //Percentage = (Income1 * RaisePrcnt1);
 //Percentage = (Income1 * RaisePrcnt2);
 //Percentage = (Income1 * RaisePrcnt3);
// Percentage = (Income1 * RaisePrcnt4);
//Percentage = (Income1 * RaisePrcnt5);





    // Calculate and print the new Raise Amount for Employee Number 1.
    //RaiseAmt1 = (Income1 * RaisePercent1) / 100;
 //printf("\n\tBased on the information you have entered for Employee Number: 1\n"); 
//  printf("\t________________________________________________________________\n");
    //printf("\n\tThe New Raise Amount is: \t$ %0.2f", RaiseAmt1); 

    // Calculate and print the New Income Amount for Employee Number 1.
    //NewPay1 = Income1 + RaiseAmt1;
    //printf("\n\tThe New Pay Amount is: \t\t$%0.2f", NewPay1); 
    //printf("\n\n");

    // Calculate and incorporate Employee 1 figures into final totals.
    //IncomeTotal = IncomeTotal + Income1;
    //RaiseTotal = RaiseTotal + RaiseAmt1;
    //PayTotal = PayTotal + NewPay1;
    /*END EMPLOYEE 1.*******************************************/

    //fflush(stdin);


    // Title for totals.
    //printf("\n\n\t\t************************************************\n");
    //printf("\t\t\tINCOME TOTALS FOR ALL EMPLOYEES"); 
    //printf("\n\t\t************************************************\n\n");

    /*Calculate and print all totals for the 4 employees.*/
    //printf("\tCurrent Yearly Incomes \tTotal: $%10.2f", IncomeTotal);
    //printf("\n\tRaise Amounts \t\tTotal: $%10.2f", RaiseTotal);
    //printf("\n\tNew Yearly Incomes \tTotal: $%10.2f", PayTotal);
    //printf("\n\n");


    system("PAUSE");
    //return (0);

} // End main.
4

2 回答 2

4

这是一个浮点精度错误这里也很好地介绍了)。仅仅因为您输入Income1为 60000.01 并不意味着Income1 >= 60000.01会是真的。

事实上,由于您目前已经构建了 if 语句,因此无需进行比较 - 一旦达到该点,您就已经知道它Income1不少于 60000,这要归功于else. 做就是了:

if(Income1 < 0){
   printf("%0.1f The Income Amount entered is INVALID. \t");
} else if (Income1 <= 60000){
       printf("%0.1f", RaisePrcnt1);
}else if (Income1 <= 70000){
          printf("%0.1f", RaisePrcnt2);
}else

等等。(另请注意,您的深度缩进是不必要的,因为它们实际上并不是嵌套的 if)。

于 2012-09-20T21:44:02.130 回答
1

浮点数的精度有限,60000.01 不完全是那个值,所以你的比较失败了。

请记住,当if失败时,您已经知道该数字不在该范围内,因此您可以简单地写:

if (Income1 < 0) {
  printf("%0.1f The Income Amount entered is INVALID. \t");
} else if (Income1 <= 60000) {
   printf("%0.1f", RaisePrcnt1);
} else if (Income1 <= 70000) {
  ...
}
于 2012-09-20T21:47:41.613 回答