【发布时间】:2015-02-06 03:41:26
【问题描述】:
我对编程还是很陌生,所以我不确定要采取的正确措施。当用户选择时,我似乎无法让程序显示不同的选项。要么显示第一个选项,要么显示“无效条目”文本。我只包含问题代码,因为我已经在没有 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 吗?
【问题讨论】:
-
我很可能会为此使用
switch声明,或者至少使用else if。
标签: c if-statement nested