2014-01-05 141 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); 
相关问题