输入值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.
答案 0 :(得分: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) {
...
}