2017-10-20 92 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; 
      } 
     } 

試試這個