【发布时间】:2012-09-26 07:50:34
【问题描述】:
我正在尝试将 if/else 嵌套在 case switch 语句中。当我输入 case 'p' 或 'P' 时,无论输入什么字符,都会打印 $15.00 行。我尝试移动/添加 {},但输出没有变化。
感谢您花时间帮助菜鸟。
完整的代码现在在这里。
#include <stdio.h>
int main()
{
//variable declarations
char typeOfWash, tireShine;
//Menu
printf("R ---> Regular ($5.00)\n");
printf("B ---> Bronze ($7.50)\n");
printf("G ---> Gold ($10.25)\n");
printf("P ---> Platinum ($15.00)\n");
printf("Tire Shine can be added to the Gold or Platinum ONLY,");
printf("for an additional$2.50\n\n");
printf("Enter your selection: ");
scanf("%c",&typeOfWash);
switch (typeOfWash)
{
case 'R': case 'r':
printf("Your bill total is: $5.00\n");
break;
case 'B': case 'b':
printf("Your bill total is: $7.50\n");
break;
case 'G': case 'g':
printf("Would you Like a Tire Shine? (Y/N): ");
scanf("%c ",&tireShine);
if (tireShine == 'Y' || tireShine == 'y')
printf("Your bill total is: $12.75\n");
else
printf("Your bill total is: $10.25\n");
break;
case 'P': case 'p':
printf("Would you Like a Tire Shine? (Y/N): ");
scanf("%c ",&tireShine);
printf("%c",tireShine);
if (tireShine == 'Y' || tireShine == 'y')
printf("Your bill total is: $17.50\n");
else
printf("Your bill total is: $15.00\n");
break;
default:
printf("Invalid Choice");
}
return 0;
}
【问题讨论】:
-
能打印出tireShine的值吗?
-
另外,你可以这样做 if ( toupper(tireShine) == 'Y' )
-
您的 15$ 或 17.5$ 取决于 Y 或 y,但不取决于 P 或 p。
-
toupper()返回您调用它时使用的 ASCII 字符的大写。 -
这是一小段代码,P 或 p 代表铂金,基础价格为 15 美元,额外的轮胎光泽度为 +2.50 美元。就像我说的,我可以进入这个案例,其他一切都很好,如果 &tireShine == 'y' 或 'Y',它只是执行 $17.50 行。
标签: c if-statement nested switch-statement