C 如何修复if语句未被识别为true
我写了我的程序,用来检查某人是否单身、已婚(分居或同居),以及他们是否是户主,以便计算他们必须缴纳的税款。由于某种原因,当我为第一次扫描键入y时,将跳过下一个if语句。请帮忙!这是我的全部代码。我知道它不是最优化的,我可以把它清理干净,但我只是想先弄清楚它到底出了什么问题C 如何修复if语句未被识别为true,c,if-statement,boolean,scanf,C,If Statement,Boolean,Scanf,我写了我的程序,用来检查某人是否单身、已婚(分居或同居),以及他们是否是户主,以便计算他们必须缴纳的税款。由于某种原因,当我为第一次扫描键入y时,将跳过下一个if语句。请帮忙!这是我的全部代码。我知道它不是最优化的,我可以把它清理干净,但我只是想先弄清楚它到底出了什么问题 void question3 (void) { char single, head, joint; int income = 0; double tax = 0; printf
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);
}
对于单个字符,应使用而不是
scanf
:
single = getchar();
如果您坚持使用scanf
:
- 您应该删除空格(因此
)scanf(“%c”,&single)
- 您可能应该测试scanf的返回:它返回它可以解析的arg的数量(在您的情况下,如果ok,它应该是1,否则应该是0)
if (single == 'y') {
...
} else if (single != 'y') {
...
}
如果你不关心单个绝对等于y或n,那么你应该简单地写下它们,如果是这样的话:
if (single == 'y') {
...
} else {
...
}
您可以使用函数来分解它,这样对于每个真正的表达式,它都会调用一个函数。在每一个函数中,你都需要收入,然后给出结果。打印结果后,调用问题3以重复该循环 对于每一个错误的表达式,您会给出另一个选择,这也会调用一个函数或给出另一个选择
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;
}
第一次打给scanf的电话不是在要求收入吗?此外,你的if语句可能应该检查收入是否小于某个金额,而不是与之完全相等。当一个简单的
else
就可以了时,没有理由使用else if(blah!=“y”)
。另外,如果收入
低于您的神奇数字(如17850
),会发生什么情况?你应该有如果Joint和last else if的代码工作正常,并给出正确的结果(分别为7332.5和9266.5),但是当我为single输入y时,它会继续问我家庭问题,如果我说y,那么它会给我最后一个else if的答案。我就是想不出怎么修复它。你应该从charhead='y'开始代码>否则,当单个
为'y'
时,没有为头
分配值。顺便说一句,编译器应该警告您这一点。不是我的DV,而是%c“
中的空格是正确的和关键的。我已经用c编写了很长时间了。但是我不记得使用了“%c”(或者我会使用getchar().我不确定当你使用C时,它是否是广为人知的知识。这是我在过去十年和一段时间里学会的东西-自从加入C以来,但很久以前-但它涉及到以前输入(收入)留下的新线。即使现在,我也大多避免使用scanf()
和fscanf()
;我优先使用fgets()
和sscanf()
或类似的东西。