-3

我正在使用 Java (NetBeans 6.8) 制作字谜游戏。代码运行时没有错误,但是当我尝试游戏时,我没有从 list/anagramlist 收到任何值(它返回“错误 - 空结果”)。我的代码有问题吗?我认为它可能在 WordList.java 中,因为这应该将值添加到列表中。也许我错误地称呼虚空?

注意:这是我的代码片段的汇编

这是我的代码:

//This is the code to add the word to the word list, and make the anagram
//and add it to the anagram list. This code is in the game creator 
//(GameCreator.java)

String toadd = (jTextField1.getText() + ",");
WordList wordlist = new WordList();
AnagramMaker anagmaker = new AnagramMaker();
wordlist.addword(toadd);
wordlist.addanag(anagmaker.makeanagram(toadd, toadd.length()));

//This is the code in the anagram maker (AnagramMaker.java) to make the anagram

char[] usedpos;
public String makeanagram(String toanagram, int length){
usedpos = null;
String anagram = "";
Random rnd = new Random();
    if(usedpos == null)
    {
        int rnum = rnd.nextInt(length);
        usedpos = (Integer.toString(rnum)).toCharArray();
        anagram += Character.toString(toanagram.toCharArray()[rnum]);
    }
    else
    {
       for(int i = 0; i < length; ++i)
       {
       int rnum = rnd.nextInt(length);
          for(int pos = 0; pos < usedpos.length; ++pos)
          {
            if(Character.toString(usedpos[pos]).equals(Integer.toString(rnum)))
            {
              i-= 1;
              pos = 0;
            }
            else
            {   
              anagram += Character.toString(toanagram.toCharArray()[rnum]);
              usedpos = (new String(usedpos) + Integer.toString(rnum)).toCharArray();
              pos = 0;
            }
          }
       }
    }
    return anagram;

//This is the code in the word list (WordList.java) to receive and add the words
//and to allow the user to get words too.

char[] list = null;
char[] anagramlist = null;

public String getword(int num){
String word = "";
int wordcount = 0;
int endpos = 0;
if(list == null)
{
    return "Error - Null Result";
}
else
{
for(int i = 0; i != -1; ++i)
{
    if((Character.toString(list[i]).equals(",")))
    {
        wordcount += 1;
        if(wordcount == (num - 1))
        {
            for(int pos = list[i]; pos > endpos; ++pos)
            {
                word += Character.toString(list[pos]);
                 if(Character.toString(list[pos + 1]).equals(","))
                 {
                     endpos = pos + 1;
                     i = -1;
                 }
            }
        }
    }
}
return word;
}
}
public String getanagram(int num){
String anagram = "";
int wordcount = 0;
int endpos = 0;
if(list == null)
{
return "Error - Null Result";
}
else
{
for(int i = 0; i != -1; ++i)
{
    if((Character.toString(anagramlist[i]).equals(",")))
    {
        wordcount += 1;
        if(wordcount == (num - 1))
        {
            for(int pos = anagramlist[i]; pos > endpos; ++pos)
            {
                anagram += Character.toString(anagramlist[pos]);
                 if(Character.toString(anagramlist[pos + 1]).equals(","))
                 {
                     endpos = pos + 1;
                     i = -1;
                 }
            }
        }
    }
}
}
return anagram;
}
public void addword(String word){
  if(list != null)
  {
    list = (new String(list) + word).toCharArray();
  }
  else
  {
    list = word.toCharArray();
  }
}

public void addanag(String anagram){
  if(anagramlist != null)
  {
    anagramlist = (new String(anagramlist) + anagram).toCharArray();
  }
  else
  {
    anagramlist = anagram.toCharArray();
  }
}

//This is the code in the game (Game.java)

int length;

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    jTextField1.setText(null);
    jLabel2.setText(null);
    newword();
}                                        

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    jLabel2.setText("The answer is: " + answer);
}                                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if(jTextField1.getText().equals(answer))
    {
     jLabel2.setText("Correct answer");
    }
    else
    {
    jLabel2.setText("Incorrect answer, try again");
    }
}                                        

public void newword(){
  jTextField1.setText(null);
  jLabel1.setText(null);
  jLabel2.setText(null);
  Random rnd = new Random();
  int rnum = rnd.nextInt(length);
  answer = wordlist.getword(rnum);
  anagram = wordlist.getanagram(rnum);
  jLabel1.setText("The anagram is: " + anagram + ". Guess the original word");
}
public void getlength(int lng){
  length = lng;
  newword();
}
4

2 回答 2

2

很酷,在方法中添加一个}afterreturn anagram;makeanagram

于 2013-10-08T19:13:19.680 回答
1

这是因为这些值没有被输入到其他类中,因此您的函数返回 null 结果

于 2013-10-14T15:02:40.487 回答