【问题标题】:Else statement inside Switch is an syntax error?Switch 中的 Else 语句是语法错误?
【发布时间】:2012-03-30 16:28:33
【问题描述】:

以下代码中的 else 语句出现语法错误:

switch(v.getId())
{
case R.id.b0 :      
    if(s.length()==1 && s.charAt(0)=='0');
    {  
    et2.setText("0");
    et1.setText("0");
    }
    else
    {
    s=s+"0";
    call(s);
    }
    break;
//2nd case and body
}

错误是:Syntax error on token "else", delete this token

java 不允许这样做吗?

我必须使用嵌套开关吗?

还是我的代码本身有问题?

【问题讨论】:

    标签: java android switch-statement if-statement


    【解决方案1】:
    if(s.length()==1 && s.charAt(0)=='0');
    

    删除;

    【讨论】:

    • 我没想到这个答案会得到nice answer 徽章:)
    【解决方案2】:
    if(s.length()==1 && s.charAt(0)=='0');
                                         ^ Remove this!
    

    您比您认为的要早终止您的if 语句...

    【讨论】:

      【解决方案3】:

      去掉if结尾的;

      应该是:

       if(s.length()==1 && s.charAt(0)=='0')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        相关资源
        最近更新 更多