2

開關殼體爲了提高水平後10 ProjectilesDestroyed,我已設置開關盒,其中用於所述第一級後10個拋射銷燬第二電平顯示,但是從第二電平其每2 projectilesDestroyed並且還增加水平超過10的增加,我應該怎樣讓它10至增加水平的差異。 這是我如何實施問題與在機器人

if (++_projectilesDestroyed > 5) 
      { 
       _projectilesDestroyed = 0; 

      // for(level=1; level<12; level++) 
       switch(level) 
        { 
        case 1: 
         _projectilesDestroyed = 10; 
        System.out.println("case 1"); 
             break; 
        case 2: 
         _projectilesDestroyed = 20; 
         System.out.println("case 2"); 
         break; 
        case 3: 
         _projectilesDestroyed = 30; 
         System.out.println("case 3"); 
         break; 
        case 4: 
         _projectilesDestroyed = 40; 
         System.out.println("case 4"); 
         break; 
        case 5: 
         _projectilesDestroyed = 50; 
             break; 
        case 6: 
         _projectilesDestroyed = 60; 
            break; 
        case 7: 
         _projectilesDestroyed = 70; 
            break; 
        case 8: 
         _projectilesDestroyed = 80; 
             break; 
        case 9: 
         _projectilesDestroyed = 90; 
            break; 
        case 10: 
         _projectilesDestroyed = 100; 
         System.out.println("case 10"); 
         break; 
        default: break; 
        } 
       addLevel(); 

addLevel()方法。

public void addLevel() { 
    level = level + 1; 
       showLevel(level); 
      } 

即使如果我加上break;所有從2級的情況下,其得到更新,每2 projectilesDestroyed,但我想,當它達到10

+0

在這種情況下,如果你根據你目前的水平只能分配值,你可以使用'_projectilesDestroyed = level * 10'或者像這樣'int [] projectilesDestroyed = {10,20,... 100}'' –

+0

預先填入一個數組如果我正確地得到你,你說你的'level'是每摧毀2架射彈增加。我認爲現在的問題不是你的代碼發佈了,而是調用'addLevel()'方法的代碼。 –

+0

你已經完成了代碼或仍然在等待答案? –

回答

0

你是case塊後失蹤突破satements。

 case 1: 
        _projectilesDestroyed = 10; 
        System.out.println("case 1"); 
        // missing break here and in the following case staments 

如果你不把break語句的情況後,再下一個case語句將順序等執行。

0

你忘了添加break陳述每個case。這將是一個倒閉。應使用

case 1: 
    _projectilesDestroyed = 10; 
    System.out.println("case 1"); 
    break; // to prevent falling through 

JLS 14.11

如果代碼是不落空的情況下,以這種方式的話,那麼break語句。

如果其中一個case常量等於表達式的值,那麼我們說case匹配,並且在switch塊中匹配case標籤之後的所有語句(如果有的話)按順序執行。

0
case 1: 
      _projectilesDestroyed = 10; 
      System.out.println("case 1"); 
    break; // missing 

你缺少對每個案例break聲明。

每個break語句結束封閉的switch語句。控制流程繼續執行開關塊後的第一條語句。該break語句是必要的,因爲沒有他們,在開關塊語句告吹:該文檔

檢查的詳細信息

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

+0

感謝您的回覆請看到我更新的帖子@Raghunandan – samm

+0

@jack你的更新並不反映添加break語句。檢查文檔和示例'SwitchDemoFallThrough'。確保你有所有情況下的休息。 – Raghunandan

+0

@jack你打電話給'addLevel'外面的開關。 – Raghunandan

0

告吹........

由於每switch docs

每個break語句都會終止封閉的switch語句。控制流程繼續執行開關塊後的第一條語句。

的break語句是必要的,因爲沒有他們,在開關塊語句告吹:

你的每一種情況下

switch(level) 
        { 
        case 1: 
         _projectilesDestroyed = 10; 
        System.out.println("case 1"); 
        break; 
+0

這隻適用於第一種情況,從第二級更新爲每2個ProjectilesDestroyed。 @Baadshah – samm

0

我猜你的問題出在之後添加break第一個if聲明:

if (++_projectilesDestroyed > 5) 

一旦你有des超過5發射彈的特洛伊木馬,你將永遠進入該區塊並增加你的level。你可能會需要一些不同的變量,在這裏,像這樣

if(++_projectilesDestroyed > _projectilesRequired) 

和設置您的_projectilesRequired根據您switch-case上述聲明或使用_projectilesRequired = level * 10