2013-05-02 19 views
0

我正在嘗試使用hashmap創建一個帶有單詞列表的簡單遊戲。我想要做的是。我想用亂碼的方式向用戶顯示一個單詞。比如說你好會是「loeh」。用戶將不得不輸入他的答案,如果答案是正確的,用戶會得到一個答案。有人可以告訴如何能夠在地圖列表中加密密鑰以顯示給用戶;顯示使用hashmap的單詞和清單的列表

這是我到目前爲止的代碼;

public class Game extends Applet { 

/* 
* (non-Javadoc) 
* 
* @see java.applet.Applet#init() 
*/ 

// create a list words with answers. probbaly a map list 


Map<String, String> words = new HashMap<String, String>();  
// add words and definition to the list 
    words.put("HI", " A form Salutation"); 
+0

請刪除代碼,因爲他們混淆了讀者和作無謂的長期問題是無關你的問題的部件 – LionC 2013-05-02 13:42:04

回答

0

這裏是我一起去的概念。

for each letter in word 
    listofletters.add(letter) 

while(listofletters.notEmpty()) 
    scrambledword += listofletters.pop(RandomNum(0,listofletters.size)) 

print scambledword 

這裏彈出既是返回信件,並從列表中刪除它。如果你把我的psuedo代碼編碼到java中,並且有錯誤,將它作爲編輯備份,並幫助你調試。

0
public String shuffle (String yourKey) { 
    char tmp; 
    char[] newStr = yourKey.toCharArray(); 
    Random r = new Random(); 
    int newPos; 
    for (int i=0; i < yourKey.length(); i++) { 
     newPos = r.nextInt(yourKey.length()); 
     tmp = newStr[i]; 
     newStr[i] = newStr[newPos]; 
     tab[newPos] = tmp; 
    } 
    return new String(newStr); 
} 

在主代碼

String newStr = shuffle(yourKey); 
while(newStr.compareTo(yourKey)==0) { 
    newStr = shuffle(yourKey); 
}