我可以允许用户在嵌套的 switch 语句中输入一个字符吗?
Can i like allow the user input a char during a nested switch statement?
就像我首先要求用户输入年份,如果他输入 2010 那么输出就会出来,如果他输入像 2012 那么他需要再次输入一个字符,我很不擅长解释但无论如何我能做到?我是 C 编程的新手..谢谢..我试图在嵌套的 switch 语句中添加 printf 和 scanf 但它不起作用...
这是我的代码,如果你能看懂的话,哈哈,我是编程新手..
#include <stdio.h>
int main() {
int year;
char code;
float cost;
printf("Enter the year : ");
scanf("%d", &year);
switch (year)
{
case 2010:
cost = 200.50;
break;
case 2012:
case 2013:
switch (code)
{
printf("Enter the code : ");
scanf("%c", &code);
case 'A':
case 'a':
cost = 89.00;
break;
case 'B':
case 'b':
cost = 105.90;
break;
default:
printf("The code entered is invalid. ");
break;
}
break;
case 2014:
cost = 350.30;
break;
default:
printf("The year you entered is invalid. ");
}
printf("The cost is : RM %.2f", cost);
}
而不是:
case 2013:
switch (code)
{
printf("Enter the code : ");
scanf("%c", &code);
case 'A':
做:
case 2013:
printf("Enter the code : ");
scanf("%c", &code);
switch (code)
{
case 'A':
就像我首先要求用户输入年份,如果他输入 2010 那么输出就会出来,如果他输入像 2012 那么他需要再次输入一个字符,我很不擅长解释但无论如何我能做到?我是 C 编程的新手..谢谢..我试图在嵌套的 switch 语句中添加 printf 和 scanf 但它不起作用...
这是我的代码,如果你能看懂的话,哈哈,我是编程新手..
#include <stdio.h>
int main() {
int year;
char code;
float cost;
printf("Enter the year : ");
scanf("%d", &year);
switch (year)
{
case 2010:
cost = 200.50;
break;
case 2012:
case 2013:
switch (code)
{
printf("Enter the code : ");
scanf("%c", &code);
case 'A':
case 'a':
cost = 89.00;
break;
case 'B':
case 'b':
cost = 105.90;
break;
default:
printf("The code entered is invalid. ");
break;
}
break;
case 2014:
cost = 350.30;
break;
default:
printf("The year you entered is invalid. ");
}
printf("The cost is : RM %.2f", cost);
}
而不是:
case 2013:
switch (code)
{
printf("Enter the code : ");
scanf("%c", &code);
case 'A':
做:
case 2013:
printf("Enter the code : ");
scanf("%c", &code);
switch (code)
{
case 'A':