switch语句错误:语句必须以case标签为前缀

时间:2017-10-20 13:30:43

标签: android switch-statement

您好我有一个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;
              }
          }

我该怎么办?感谢

2 个答案:

答案 0 :(得分:0)

就像@Samuel Roberts在评论中所说,你需要搬家:

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

位于case 2内(因此也可能位于其他情况下)或switch之外。您收到的错误是"您只能在case内发出switch声明,如果不在case内,则不允许使用其他声明&#34 ;

请参阅此处的文档:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

希望这有帮助!

答案 1 :(得分:0)

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;
          }
      }

试试这个