【问题标题】:switch statement Error : Statement must be prepended with case labelswitch 语句错误:必须在语句前面加上 case 标签
【发布时间】:2017-10-20 13:30:43
【问题描述】:

您好,我有一个 switch 语句,我想将 intent.getIntExtra("Position",0) 值保存到 Position 变量但是当我这样做时给我提到的错误,这是我的代码

if(intent.getIntExtra("HandyLevel",0)==1 && SharedPreferenceStuff.getLevel(getApplicationContext())>=1) //Preface
        {
            HandyLevel = intent.getIntExtra("HandyLevel",0);
            switch (intent.getIntExtra("Position",0))
            {
                int Positions = intent.getIntExtra("Position",0);

                case 2: //History
                    if(intent.getStringExtra("Divider").equals("Q1History" )) {

                        if(goToNextLevel) {
                            if (SharedPreferenceStuff.getSubLevel(getApplicationContext()) == 3)
                                SharedPreferenceStuff.setSubLevel(getApplicationContext(), 4);
                            localIntent = new Intent(QuestionFrame.this, LevelOne.class);
                            localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(localIntent);
                            overridePendingTransition(R.anim.slide_start_from_button, R.anim.slide_to_up);
                        }
                        else Toast.makeText(getApplicationContext(),"FUCCCK",Toast.LENGTH_LONG).show();
                    }
                    break;
              }
          }

我该怎么办?谢谢

【问题讨论】:

  • 为什么要使用switch语句?一个 case 的 switch 不是一个过于复杂的 if 语句吗?
  • int Positions = intent.getIntExtra("Position",0);放在case 2声明下
  • 是的,但这不是我的全部代码,我只是复制了一部分:) @Ryry
  • 感谢@SamuelRobert 发布,然后我接受它作为答案

标签: android switch-statement


【解决方案1】:

就像@Samuel Roberts 在 cmets 中所说,你需要移动:

int Positions = intent.getIntExtra("Position",0);

在您的case 2 内(因此也可能在您的其他情况下)或在您的switch 之外。您遇到的错误是“您只能在 switch 中使用 case 语句,而在 case 中不允许使用其他语句”。

请在此处查看文档:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

希望这会有所帮助!

【讨论】:

    【解决方案2】:
    if(intent.getIntExtra("HandyLevel",0)==1 && SharedPreferenceStuff.getLevel(getApplicationContext())>=1) //Preface
        {
            HandyLevel = intent.getIntExtra("HandyLevel",0);
            int Positions = intent.getIntExtra("Position",0);
            switch (intent.getIntExtra("Position",0))
            {
                 case 2: //History
                    if(intent.getStringExtra("Divider").equals("Q1History" )) {
    
                        if(goToNextLevel) {
                            if (SharedPreferenceStuff.getSubLevel(getApplicationContext()) == 3)
                                SharedPreferenceStuff.setSubLevel(getApplicationContext(), 4);
                            localIntent = new Intent(QuestionFrame.this, LevelOne.class);
                            localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(localIntent);
                            overridePendingTransition(R.anim.slide_start_from_button, R.anim.slide_to_up);
                        }
                        else Toast.makeText(getApplicationContext(),"FUCCCK",Toast.LENGTH_LONG).show();
                    }
                    break;
              }
          }
    

    试试这个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      相关资源
      最近更新 更多