我對編程仍然很陌生,所以我不確定採取的正確操作。我似乎無法讓程序在用戶選擇時顯示不同的選項。顯示第一個選項,或顯示「Invalid Entry」文本。我只會包含問題代碼,因爲我已經測試了其餘的if/else語句,並且它可以正確計算和顯示。在C語言中正確使用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'。 – 2015-02-06 03:57:02