2013-01-10 96 views
0
public static String[] getWords(int cat, int diff) { 
    String topic[][][] = new String[3][3][3]; 
    switch(cat){ 
     case 0: 
      topic[0][0][0] = "Paris"; 
      topic[0][0][1] = "London"; 
      topic[0][0][2] = "Sydney"; 
      diff = 0; 
      topic[0][1][0] = "Toronto"; 
      topic[0][1][1] = "Florida"; 
      topic[0][1][2] = "Frankfurt"; 
      diff = 1; 
      topic[0][2][0] = "Barcelona"; 
      topic[0][2][1] = "Vancouver"; 
      topic[0][2][2] = "Zimbabwe"; 
      diff = 2; 
     case 1: 
      topic[1][0][0] = "Halo"; 
      topic[1][0][1] = "Fifa";     
      topic[1][0][2] = "GTA"; 
      diff = 0;     
      topic[1][1][0] = "Skyrim"; 
      topic[1][1][1] = "HITMAN"; 
      topic[1][1][2] = "Batman"; 
      diff =1; 
      topic[1][2][0] = "Minecraft"; 
      topic[1][2][1] = "Zombieville"; 
      topic[1][2][2] = "BoderLands"; 
      diff =2; 
     case 2: 
      topic[2][0][0] = "Acura"; 
      topic[2][0][1] = "Audi"; 
      topic[2][0][2] = "Bmw"; 
      diff = 0; 
      topic[2][1][0] = "Bentley"; 
      topic[2][1][1] = "Buggati"; 
      topic[2][1][2] = "Honda"; 
      diff = 1; 
      topic[2][2][0] = "Lamborghini"; 
      topic[2][2][1] = "Rolls-Royce"; 
      topic[2][2][2] = "Mercedes"; 
      diff = 2; 
    } 
    return topic[cat][diff]; 
} 

所以這我的方法與3D陣列,我想知道如果我這樣做是正確的,並且如果我在main方法中調用它能否行得通?我用開關語句,因爲有人推薦我,我真的是新的Java,因爲你可以看到 這是遊戲Hang子手3D陣列不工作

+4

您是否注意到您在每個案例結束時沒有「break」聲明?這是不太可能被期望的。我們無法確定它是否可行,因爲我們不知道它正在努力達成什麼 - 但它不可能是實現這一目標的最佳方式,IMO。 –

+0

在主要方法中,將詢問他們想要玩什麼類別和難度級別以及他們選擇的任何內容將讀入該方法並轉到該部分,您如何做到這一點 –

回答

3

不,它不會。您的交換機不包含任何break;語句,因此在它符合第一個case其計算結果爲true後,它將執行後面的所有語句,直到交換機結束,或者直到您break;return爲止。

此外,呼叫:

diff = 0; 
// ... 
diff = 1; 
// ... 
diff = 2; 

是多餘的,因爲分配的值從未使用過。

所以它應該是:

switch(cat) 
{ 
    case 0: 
     topic[0][0][0] = "Paris"; 
     topic[0][0][1] = "London"; 
     topic[0][0][2] = "Sydney"; 

     topic[0][1][0] = "Toronto"; 
     topic[0][1][1] = "Florida"; 
     topic[0][1][2] = "Frankfurt"; 

     topic[0][2][0] = "Barcelona"; 
     topic[0][2][1] = "Vancouver"; 
     topic[0][2][2] = "Zimbabwe"; 
     break; 
    case 1: 
     topic[1][0][0] = "Halo"; 
     topic[1][0][1] = "Fifa"; 
     topic[1][0][2] = "GTA"; 

     topic[1][1][0] = "Skyrim"; 
     topic[1][1][1] = "HITMAN"; 
     topic[1][1][2] = "Batman"; 

     topic[1][2][0] = "Minecraft"; 
     topic[1][2][1] = "Zombieville"; 
     topic[1][2][2] = "BoderLands"; 
     break; 
    case 2: 
     topic[2][0][0] = "Acura"; 
     topic[2][0][1] = "Audi"; 
     topic[2][0][2] = "Bmw"; 

     topic[2][1][0] = "Bentley"; 
     topic[2][1][1] = "Buggati"; 
     topic[2][1][2] = "Honda"; 

     topic[2][2][0] = "Lamborghini"; 
     topic[2][2][1] = "Rolls-Royce"; 
     topic[2][2][2] = "Mercedes"; 
     break; 
} 
+0

比您如何在主要方法中調用它就像用戶說他們想要玩的類別是汽車一樣,而困難很容易,你會怎麼做?如果它會是這樣的 –

+0

@ N00b_JAVA你只問他們想要一次又一次的困難嗎?因爲如果是這樣,它應該在交換機之外。或者你是否說每個區塊都有不同的難度? – MrLore

+0

只要我50美分, 不要在方法中使用「靜態」結構,你可以將這個數組移動到靜態域中。 – iMysak

1

你需要休息;在案件之間。由於沒有休息,它只是簡單地通過每一個案例。那麼每種情況下,如果它的情況1,和2 & 3,如果它是2,而只是3.

所以3是唯一一個會正常破壞,因爲它是最後一個你不需要一個。