若有定义:float x=1.5;int a=1,b=3,c=2;则正确的switch语句是( )。
A、
switch(a+b)
{case 1:pr4nff(“*\n”);
case 2+1:printf(“**\n”);}
B、
switch((int)x);
{case 1:prinff(“*\n”);
case 2:printf(“**\n”);}
C、
switch(x)
{case 1.0:prinff(“*\n”);
case 2.0:prinff(“**\n”);}
D、
switch(a+b)
{ease 1:printf(“*\n”);
ease c:printf(“*\n”);}
【正确答案】
A
【答案解析】
B选项中switch((int)x);语句中不应该有最后的分号。switch(expr1),中的expr1不能用浮点类型或long类型,也不能为一个字符串,所以C错误。case后面常量表达式的类型必须与switch后面表达式的类型一致,所以D错误。
提交答案
关闭