输一个字符,如果输入字符为c或C时,输出Cancel;
如果为y或Y时,输出Yes;如果为n或N时,输出Yes;
否则输出Illegal;
法一:
#include<stdio.h>
int main()
{
char c=getchar();
switch(c)
{
case 'c':
printf("Cancle\n");
break;
case 'C':
printf("Cancle\n");
break;
case 'y':
printf("Yes\n");
break;
case 'Y':
printf("Yes\n");
break;
case 'n':
printf("No\n");
break;
case 'N':
printf("No\n");
break;
default:
printf("Illegal\n");
break;
}
return 0;
}
法二:(简化后)
#include<stdio.h>
int main()
{
char c=getchar();
switch(c)
{
case 'c':
case 'C':
printf("Cancle\n");
break;
case 'y':
case 'Y':
printf("Yes\n");
break;
case 'n':
case 'N':
printf("No\n");
break;
default:
printf("Illegal\n");
break;
}
return 0;
}
(输入的字符‘c’,则从case ‘c’:开始向下运行知道遇见break,跳出switch语句;即:switch的‘穿透’;)