当前位置: 技术问答>java相关
关于 swich 语句的一个疑问,请帮忙看一下。
来源: 互联网 发布时间:2015-05-06
本文导语: 为什么在编译以下这个switch 语句时,switch 的变量必须是 不是operator.charAt(0),如果我换成 operator时,编译就同不过,请问这是为什么? 谢谢!! static int precedence(String operator) { switch(operator.charAt(0)...
为什么在编译以下这个switch 语句时,switch 的变量必须是
不是operator.charAt(0),如果我换成 operator时,编译就同不过,请问这是为什么? 谢谢!!
static int precedence(String operator)
{
switch(operator.charAt(0))
{
case '(':
return 0;
case '-':
return 1;
case '+':
return 1;
case '*':
return 2;
case '/':
return 3;
case ')':
return 4;
}
return 0;
}
不是operator.charAt(0),如果我换成 operator时,编译就同不过,请问这是为什么? 谢谢!!
static int precedence(String operator)
{
switch(operator.charAt(0))
{
case '(':
return 0;
case '-':
return 1;
case '+':
return 1;
case '*':
return 2;
case '/':
return 3;
case ')':
return 4;
}
return 0;
}
|
switch
The switch is sometimes classified as a selection statement. The switch statement selects from among pieces of code based on the value of an integral expression. Its form is: [ Add Comment ]
switch(integral-selector) {
case integral-value1 : statement; break;
case integral-value2 : statement; break;
case integral-value3 : statement; break;
case integral-value4 : statement; break;
case integral-value5 : statement; break;
// ...
default: statement;
}
Integral-selector is an expression that produces an integral value. The switch compares the result of integral-selector to each integral-value. If it finds a match, the corresponding statement (simple or compound) executes. If no match occurs, the default statement executes
The switch is sometimes classified as a selection statement. The switch statement selects from among pieces of code based on the value of an integral expression. Its form is: [ Add Comment ]
switch(integral-selector) {
case integral-value1 : statement; break;
case integral-value2 : statement; break;
case integral-value3 : statement; break;
case integral-value4 : statement; break;
case integral-value5 : statement; break;
// ...
default: statement;
}
Integral-selector is an expression that produces an integral value. The switch compares the result of integral-selector to each integral-value. If it finds a match, the corresponding statement (simple or compound) executes. If no match occurs, the default statement executes
|
你传入的参数是String,
而你比较的参数是char,
当然不行啦!
如果你想用operator,
请将static int precedence(String operator)
改为static int precedence(char operator)
并在传入参数入char型.
而你比较的参数是char,
当然不行啦!
如果你想用operator,
请将static int precedence(String operator)
改为static int precedence(char operator)
并在传入参数入char型.
|
同意楼上,switch语句不支持字符串
|
我也同意楼上的,switch语句不支持字符串
|
呵呵 要是c#就可以向你那样
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。