我正在使用Java(NetBeans 6.8)制作一个字谜游戏。代码在没有错误的情况下运行,但是当我尝试游戏时,我没有从list / anagramlist中收到任何值(它返回'Error - Null Result')。我的代码有问题吗?我认为它可能在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();
}
答案 0 :(得分:2)
酷,在}
方法
return anagram;
之后添加makeanagram
答案 1 :(得分:1)
这是因为没有将值输入到其他类中,因此您的函数返回null结果