2017-10-20 213 views
-3

你好,我有一个switch语句,我想不过来保存intent.getIntExtra("Position",0)价值Position变量,当我做到这一点给我提到的错误,这里是我的代码switch语句错误:语句必须以case标签被前置

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

我该怎么办?由于

+0

你为什么要使用switch语句?不是一个开关只是一个复杂的if语句吗? –

+3

put'int Positions = intent.getIntExtra(“Position”,0);'case 2''语句下的 –

+0

yes是的,但这不是我的代码的全部,我只是复制它的一部分:) @Ryry –

回答

0

像@Samuel罗伯茨说,在评论中,你需要移动:

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

要么case 2(因此有可能在你的其他情况下也是如此)或你的switch你的外内。您收到的错误是“您只能在switch内有case声明,其他声明不能在case之内”。

请在这里看到的文档:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

希望这有助于!

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

试试这个