我编写的程序应该检查某人是否单例、已婚(分居或 union )以及他们是否是一家之主,以便计算他们必须缴纳的税款。由于某种原因,当我为第一个 scanf 输入 y 时,下一个 if 语句将被跳过。请帮忙!这是我的整个代码。我知道它不是最优化的,我可以清理它,但我只是想先找出它出了什么问题。
void question3 (void)
{
char single, head, joint;
int income = 0;
double tax = 0;
printf("What is your total income?");
scanf("%d", &income);
printf ("Are you single? (y/n) \n");
scanf (" %c", &single);
if (single == 'y')
{
if (income == 17850)
{
tax = income * .15;
}
else if (income >= 17850)
{
tax = ((.15 * 17850) + (.28 * (income - 17850)));
}
}
else
{
if (single != 'y')
{
printf("Are you the head of the house hold? y or n\n");
scanf(" %c", &head);
}
}
if (head == 'y')
{
if (income == 23900)
{
tax = income * .15;
}
else if (income >= 23900)
{
tax = ((.15 * 23900) + (.28 * (income - 23900)));
}
}
else if (head != 'y')
{
printf("Do you have a joint marriage\n");
scanf(" %c", &joint);
}
if (joint == 'y')
{
if (income == 29750)
{
tax = income * .15;
}
else if (income >= 29750)
{
tax = ((.15 * 29750) + (.28 * (income - 29750)));
}
}
else if (joint != 'y')
{
if(income == 14875)
{
tax = income * .15;
}
else if (income >= 14875)
{
tax = ((.15 * 14875) + (.28 * (income - 14875)));
}
}
printf("You owe %f dollars in taxes.", tax);
}
最佳答案
您可以使用函数来分解它,这样对于每个真实的表达式,它都会调用一个函数。在每个函数中,您都要求收入,然后给出结果。打印结果后,它调用 Question3 来重复循环。
对于每个错误表达式,您都会给出另一个选择,这也可以调用函数或给出另一个选择。
char single, head, joint;
int income = 0;
double tax = 0;
void Single();
void Head();
void Joint();
void question3(void)
{
printf("Are you single? (y/n) \n");
scanf(" %c", &single);
if (single == 'y')
{
Single();
}
else if (single == 'n')
{
printf("Are you the head of the house hold? y or n\n");
scanf(" %c", &head);
if (head == 'y')
{
Head();
}
else if (head == 'n')
{
printf("Do you have a joint marriage\n");
scanf(" %c", &joint);
if (joint == 'y')
{
Joint();
}
else if(joint == 'n')
{
question3();
}
}
}
}
void Single()
{
printf("What is your total income?");
scanf("%d", &income);
if (income == 17850)
{
tax = income * .15;
}
else if (income >= 17850)
{
tax = ((.15 * 17850) + (.28 * (income - 17850)));
}
printf("You owe %f dollars in taxes.\n\n", tax);
question3();
return 0;
}
void Head()
{
printf("What is your total income?");
scanf("%d", &income);
if (income == 23900)
{
tax = income * .15;
}
else if (income >= 23900)
{
tax = ((.15 * 23900) + (.28 * (income - 23900)));
}
printf("You owe %f dollars in taxes.\n\n", tax);
question3();
return 0;
}
void Joint()
{
printf("What is your total income?");
scanf("%d", &income);
if (income == 29750)
{
tax = income * .15;
}
else if (income >= 29750)
{
tax = ((.15 * 29750) + (.28 * (income - 29750)));
}
printf("You owe %f dollars in taxes.\n", tax);
question3();
return 0;
}
void main()
{
question3();
return 0;
}
关于c - 如何解决我的 if 语句不被识别为 true 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58331814/