错,去看看JAVA的语法吧. 1.switch(n),n必须是int或除long之外的整数 2.case 后面只能有一个表达式,不能有几个. 改成 string c = .....; int intC = Integer.parseInt(c); switch(intC) { case 1: 代码 break; case 2: 代码 break; case 3: case 4: case 5: 代码 break; }
switch中不能使用String型。可使用char、int型。 char i = 0; switch(c){ case 0: break; case 1: break; }
char的本质为数字类型。The switch statement transfers control to one of several statements depending on the value of an expression.SwitchStatement: switch ( Expression ) SwitchBlockSwitchBlock: { SwitchBlockStatementGroupsopt SwitchLabelsopt }SwitchBlockStatementGroups: SwitchBlockStatementGroup SwitchBlockStatementGroups SwitchBlockStatementGroupSwitchBlockStatementGroup: SwitchLabels BlockStatementsSwitchLabels: SwitchLabel SwitchLabels SwitchLabelSwitchLabel: case ConstantExpression : default :The type of the Expression must be char, byte, short, or int, or a compile-time error occurs.
To zhg_dragon(阿龙) 上面的这段话来自《Java Language Specification Second Edition》http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.htmlTo 楼主 你把字符串转为字符数组可以,但是这样还是不满足条件,要char不能是char[]。so 用我前面说的吧。
1.switch(n),n必须是int或除long之外的整数
2.case 后面只能有一个表达式,不能有几个.
改成
string c = .....;
int intC = Integer.parseInt(c);
switch(intC)
{
case 1:
代码
break;
case 2:
代码
break;
case 3:
case 4:
case 5:
代码
break;
}
char i = 0;
switch(c){
case 0: break;
case 1: break;
}
switch ( Expression ) SwitchBlockSwitchBlock:
{ SwitchBlockStatementGroupsopt SwitchLabelsopt }SwitchBlockStatementGroups:
SwitchBlockStatementGroup
SwitchBlockStatementGroups SwitchBlockStatementGroupSwitchBlockStatementGroup:
SwitchLabels BlockStatementsSwitchLabels:
SwitchLabel
SwitchLabels SwitchLabelSwitchLabel:
case ConstantExpression :
default :The type of the Expression must be char, byte, short, or int, or a compile-time error occurs.
上面的这段话来自《Java Language Specification Second Edition》http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.htmlTo 楼主
你把字符串转为字符数组可以,但是这样还是不满足条件,要char不能是char[]。so 用我前面说的吧。