C语言中正确使用if/else语句的问题
Issues with using if/else statements correctly in C language
我对编程还是很陌生,所以我不确定要采取的正确措施。我似乎无法让程序在用户选择时显示不同的选项。要么显示第一个选项,要么显示 'Invalid Entry' 文本。我只打算包括问题代码,因为我已经在没有 if/else 语句的情况下测试了其余代码,并且它计算和显示正确。
printf("Select interest type: S, Y, M \n\n");
scanf("%ch", &type); /*program has finished calculating and is waiting on user input. Variable 'type' has already been initialized as a char*/
printf("\n\nIn %i years, at %.2f percent interest rate, \n", n, R);
/*this is where the problem starts*/
if (type == 'S')
printf("Your investment becomes %.2f dollars, with simple interest.\n", futureVal_simp);
else
{
if (type == 'Y')
printf("Your investment becomes %.2f dollars, with annual compounding interest.\n", futureVal_yr);
else
{
if (type == 'M')
printf("Your investment becomes %.2f dollars, with monthly compounding interest.\n\n\n", futureVal_mnth);
else printf("Invalid entry.\n\n\n"); /*these are supposed to display based on char entered*/
}
}
return 0;
}
我检查了网站上的其他问题,但仍然不确定。我应该使用 != 和 && 而不是多个 if/else 吗?
你想要 scanf("%c", &type);
而不是 "%ch"
。 %c
表示字符,h
表示文字 h
.
您还需要检查 scanf()
的 return 值。总是。
使用逻辑运算符/if-else 语句——如果它们等价,您可以选择其中之一。 (也许在这种情况下,您也可以使用 switch 语句。)
但有时,使用太长的逻辑公式作为条件会降低代码的可读性。
if(type == 'S')
{
content...
}
else if(type == 'Y')
{...}
else if(type == 'M')
{...}
else{...}
因为 else if 表示 else{if(...)} 本身,所以您不需要在 else 块中编写另一个 if/else 语句。
我推荐的最好方法是在这种情况下使用 switch 语句。分支条件并不复杂——这些条件只是检查字符 'type' 是否为 'S'、'Y'、'M' 或其他。在这种情况下,switch 语句可以增加代码的可读性。
您已经从 @John Zwinck 先生那里得到了答案,但为了完整起见,
您应该将 scanf 从
更改为
scanf("%ch", &type);
到
scanf(" %c", &type); // note the extra space before %c
这告诉 scanf()
忽略所有以前的类似空白的字符并读取第一个非空白输入。
仅供参考,在前面的例子中,先前按下的 ENTER 击键 [在先前输入之后] 在输入缓冲区中存储为 \n
。然后,\n
作为 %c
的 有效 输入,被 scanf()
读取,产生场景
'%c' it doesn't ask for input at all.
此外,作为改进,您可以考虑使用 switch 语句代替 if-else 条件。
我对编程还是很陌生,所以我不确定要采取的正确措施。我似乎无法让程序在用户选择时显示不同的选项。要么显示第一个选项,要么显示 'Invalid Entry' 文本。我只打算包括问题代码,因为我已经在没有 if/else 语句的情况下测试了其余代码,并且它计算和显示正确。
printf("Select interest type: S, Y, M \n\n");
scanf("%ch", &type); /*program has finished calculating and is waiting on user input. Variable 'type' has already been initialized as a char*/
printf("\n\nIn %i years, at %.2f percent interest rate, \n", n, R);
/*this is where the problem starts*/
if (type == 'S')
printf("Your investment becomes %.2f dollars, with simple interest.\n", futureVal_simp);
else
{
if (type == 'Y')
printf("Your investment becomes %.2f dollars, with annual compounding interest.\n", futureVal_yr);
else
{
if (type == 'M')
printf("Your investment becomes %.2f dollars, with monthly compounding interest.\n\n\n", futureVal_mnth);
else printf("Invalid entry.\n\n\n"); /*these are supposed to display based on char entered*/
}
}
return 0;
}
我检查了网站上的其他问题,但仍然不确定。我应该使用 != 和 && 而不是多个 if/else 吗?
你想要 scanf("%c", &type);
而不是 "%ch"
。 %c
表示字符,h
表示文字 h
.
您还需要检查 scanf()
的 return 值。总是。
使用逻辑运算符/if-else 语句——如果它们等价,您可以选择其中之一。 (也许在这种情况下,您也可以使用 switch 语句。) 但有时,使用太长的逻辑公式作为条件会降低代码的可读性。
if(type == 'S')
{
content...
}
else if(type == 'Y')
{...}
else if(type == 'M')
{...}
else{...}
因为 else if 表示 else{if(...)} 本身,所以您不需要在 else 块中编写另一个 if/else 语句。
我推荐的最好方法是在这种情况下使用 switch 语句。分支条件并不复杂——这些条件只是检查字符 'type' 是否为 'S'、'Y'、'M' 或其他。在这种情况下,switch 语句可以增加代码的可读性。
您已经从 @John Zwinck 先生那里得到了答案,但为了完整起见,
您应该将 scanf 从
更改为 scanf("%ch", &type);
到
scanf(" %c", &type); // note the extra space before %c
这告诉 scanf()
忽略所有以前的类似空白的字符并读取第一个非空白输入。
仅供参考,在前面的例子中,先前按下的 ENTER 击键 [在先前输入之后] 在输入缓冲区中存储为 \n
。然后,\n
作为 %c
的 有效 输入,被 scanf()
读取,产生场景
'%c' it doesn't ask for input at all.
此外,作为改进,您可以考虑使用 switch 语句代替 if-else 条件。