2013-10-20 23 views
0

我目前正在java中製作一個懸掛式遊戲,並且我有一個方法中按難度組織的單詞列表,然後在另一個方法中,我有一個if語句詢問用戶想玩的難度。我將如何去做這件事?我的代碼如下:每每個難度如何以不同的方法從數組列表中選擇案例

public static int word(){ 
String words[] = new String[26]; 
    switch(diff){ 
    case 1: 
     words[0] = "cat"; 
     words[1] = "dog"; 
     words[2] = "book";   
     words[3] = "breakfeast";   
     words[4] = "telephone";   
     words[5] = "mixture";   
     words[6] = "music";   
     words[7] = "animal";   
     words[8] = "school";   
     words[9] = "plant";   
     words[10] = "pen";   
     words[11] = "pencil";   
     words[12] = "paper";   
     words[13] = "note";   
     words[14] = "fog";   
     words[15] = "smoke";   
     words[16] = "bake";   
     words[17] = "alone";   
     words[18] = "drive";   
     words[19] = "town";   
     words[20] = "city";   
     words[21] = "sunny";   
     words[22] = "shine";   
     words[23] = "polish";   
     words[24] = "cap";   
     words[25] = "hat"; 

     break; 
    case 2: 
     words[0] = "president"; 
     words[1] = "exclamation";   
     words[2] = "statement";   
     words[3] = "television";   
     words[4] = "physics";   
     words[5] = "algebra";   
     words[6] = "geometry";   
     words[7] = "difficult";   
     words[8] = "extreme";   
     words[9] = "procedure";   
     words[10] = "ship";   
     words[11] = "soldier";   
     words[12] = "lunch";   
     words[13] = "hockey";   
     words[14] = "tennis";   
     words[15] = "soccer";   
     words[16] = "football";   
     words[17] = "basketball";   
     words[18] = "bias";   
     words[19] = "magazine";   
     words[20] = "computer";   
     words[21] = "internet";   
     words[22] = "allegedly";   
     words[23] = "system";   
     words[24] = "unison";   
     words[25] = "excited";   
     break; 
    case 3: 
     words[0] = "amalgamation";   
     words[1] = "proclomation";   
     words[2] = "establishment";   
     words[3] = "rehabilitation";   
     words[4] = "rhinoceros";   
     words[5] = "velociraptor";   
     words[6] = "declaration";   
     words[7] = "announcement";   
     words[8] = "binomial";   
     words[9] = "polynomial";   
     words[10] = "congregation";   
     words[11] = "obligation";   
     words[12] = "structure";   
     words[13] = "description";   
     words[14] = "perscription";   
     words[15] = "subscribe";   
     words[16] = "address";   
     words[17] = "township";   
     words[18] = "mischievous";   
     words[19] = "bewildered";   
     words[20] = "accusation";   
     words[21] = "designation";   
     words[22] = "disgusting";   
     words[23] = "prolonged";   
     words[24] = "restoration";   
     words[25] = "regeneration";   
    } 

int i = words.length; 

Random rng = new Random();  //This block of code chooses random word from array list 
int choice = rng.nextInt(words.length); //Varible storing random word 
String wd = words[choice]; 
out.println(wd); 
} 



public static int gameStart(){ 
    Scanner qwe = new Scanner(in); 
    out.println("Welcome to my Hang Man game!\n"); 
    out.println("What difficulty would you like to play on?\t1-3"); 
    int diff = qwe.nextInt(); 

    if (diff == 1){ 
     //not sure what would go here 
    } 
    else if (diff == 2){ 
     //not sure what would go here 
    } 
    else{ 
     //not sure what would go here 
    } 


} 

回答

2
  1. 商店的話在一個單獨的文本文件。
  2. 創建一個方法String[] getWords(int difficulty)返回給您一個從文件中加載的單詞列表,給定難度。
  3. 不要在一種方法中混合處理程序不同方面的代碼 - 就像您在word()方法中所做的那樣。創建一個單詞列表,選擇隨機詞並在屏幕上顯示它聽起來像3個不同的方面。這意味着大致有3種方法。
+0

+ 1用於提示將應用程序邏輯從數據中分離出來。 –

+0

好吧,所以說我做了3個不同的文本文件名爲words1,words2和words3。在IF語句中,如果用戶輸入'1',它將使用名爲words1的文本文件並從該列表中選擇一個隨機單詞。我會怎麼做? 另外,在文本文件中,每個單詞都應該放在一個新行上? –

+0

在'getWords()'你需要一個iff,它會選擇正確的文件讀取。然後你需要從該文件中獲取所有單詞。是的,如果爲此目的最簡單的佈局,我會說每行一個作品。 – pajton

0

您需要該0123'方法的參數,並且您需要將該方法中的單詞作爲字符串(不是int)返回。例程的其餘部分(case語句和隨機單詞選擇)都可以,所以我沒有重複下面的內容。也許是這樣的:

public static String word(int diff) { 
    .... 
    return wd; 
} 

然後下來,你必須if塊,你沒有,如果塊需要的,只是度過難關納入常規。類似於:

String myWord = word(diff);