2014-01-05 138 views
0

我正在嘗試編寫一個程序,該程序是生成隨機字母的遊戲,用戶選擇是否隨機生成的字母是用戶名的一部分。我還沒有開始第二個算法,我想專注於第一個算法。如何通過循環將字符串添加到字符串數組列表

更新:嘿,所以我跟着你們的建議,我能夠得到算法1的工作。現在我完成算法1的唯一方法是有一個循環來檢查隨機生成的字母是否在錯誤的數組列表中。如果是,那麼它會生成另一個隨機字母,如果它不是arrayList的一部分,它會詢問用戶是否它的下一個字母。 wrongLetters arraylist應該包含幾個字符串(字母),這些字符串不是用戶名中的下一個字母。只要一個字母被正確猜出,它就會清除錯誤的字母arraylist並在猜測下一個字母后添加新的字符串。

我嘗試使用do/while循環使用while(actualLetter != wrongLetters);但我不斷收到有關「不兼容操作數類型」的錯誤。我怎樣才能解決這個問題?再次感謝。再次

謝謝!

/* 
* PSEUDOCODE: 
* -Ask user to choose an option (algorithm 1, algorithm 2, quit) 
* -if user selects algorithm 1, generate a random number to pass off as unicode(char) and then  change char to string 
* -ask user whether generated char is part of their name 
* -if yes, then add (actualLetter) string to arrayList(name). 
* -if no, then add (actualLetter) string to arrayList(wrongLetters). 
* reask user to choose an option (algorithm 1, algorithm 2, quit) 
* whether user chooses algorithm 1 or 2, after generating random char, check char to make sure its not in the arrayList(wrongLetters) 
* if char is in the wrongLetters arrayList, then generate another random char until a char that is not in that array generates 
* 
* 
*/ 


import javax.swing.JOptionPane; 

import java.util.ArrayList; 

public class Lab_1 { 

public static void main(String[] args) { 
    int n; 
    ArrayList<String> name = new ArrayList<String>(); 
    ArrayList<String> wrongLetters = new ArrayList<String>(); 
    String result = ""; 
    int p = 1; 
    Object[] options2 = {"No", "Yes"}; 


    do{ 
     do{ 
      Object[] options = {"Quit", "Algorithm 2", "Algorithm 1"}; 

      n = JOptionPane.showOptionDialog(null, "Please choose an option.", "A Question", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); 

      if (n != 0){ 
       if (n == 1){ 
        JOptionPane.showMessageDialog(null, "You have chosen algorithm 2."); 
       } 
       else if (n == 2){ 
        algorithm1(name, wrongLetters); 
        p = JOptionPane.showOptionDialog(null, "Are there any letters remaining in your name?", "A Question", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options2, options2[0]); 

        //if the answer is no 
        if(p == 0){ 
        for(String str: name){ 
          result=result + str; 
         } 
        JOptionPane.showMessageDialog(null, "Your name is " + result); 
         } 
        } 
       } 
      else { 
      JOptionPane.showMessageDialog(null, "Thanks for playing, " + result + "!"); 
      } 
     }while(p == 1); 
    }while (n != 0); 
} 

public static void algorithm1(ArrayList<String> name, ArrayList<String> wrongLetters){ 
    String actualLetter = ""; 
    do{ 
    int unicode = (int) (Math.random() * 25) + 65; //comes up with random number to use as a char 
    char aLetter = (char) unicode; 
    actualLetter = Character.toString(aLetter); 
    Object[] options = {"No", "Yes"}; 
    int o; 
    o = JOptionPane.showOptionDialog(null, "Is this the next letter in your name: " + aLetter, "A Question", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); 
    //if you choose yes 
    if(o == 1){ 
     name.add(actualLetter); 
     } 
    //if you choose no 
    else if (o == 0){ 
     wrongLetters.add(actualLetter); 
    } 
    }while(actualLetter != wrongLetters); 
} 

}

+0

我想我是用他們的時候,我做了'name.add(actualLetter);'和'也wrongLetters.add(actualLetter);',我做這錯了嗎? – user655321

+0

他/她的意思是,你只是在'wrongLetters'中添加'actualLetter',而不是在任何地方檢索它。 – ADTC

回答

0

你存儲在在algorithm1局部變量的ArrayList的字母。一旦你到達該函數的結尾,你將失去兩個列表以及之前存儲在其中的任何值。

我不知道這是否是一個優雅的解決方案,但您可以在您的主要方法中聲明兩個ArrayLists,然後將它們傳遞給您的算法方法。

你需要改變方法簽名algorithm1像這樣:

public static void algorithm1(ArrayList<String> name, ArrayList<String> wrongLetters) 

,然後更改您的來電算法1到這一點,假設你命名你的ArrayList namewrongLetters分別爲:

algorithm1(name, wrongLetters); 
1
JOptionPane.showMessageDialog(null, name); 

這句話如果錯了,它不能顯示名字中的名字,因爲名字是變量名單,而不是字符串。 你可以這樣做:

String result=""; 
for(String str: name){ 
    result=result+";"+str; 
} 
JOptionPane.showMessageDialog(null, result); 
相關問題