2014-11-07 39 views
1

我必须让一个字扰频器“游戏”,我有我需要使用的所有方法。我仍然需要将代码放在主要方法中,让用户猜测并使用.equals来查看它是否合适。这不是问题。我遇到了一个关于我的scrambleWord(word)方法的问题。我使用了字符串生成器,并不知道如何返回。该代码看起来不错,并建立,但说有一个错误。谁能帮忙?这是我现在的代码。麻烦从字符串生成器返回类型的单词争夺游戏的最终方法

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
import java.util.Random; 

/** 
* 
* @author Brian2 
*/ 
public class Project3 { 

/** 
* @param args the command line arguments 
* @throws java.io.IOException 
*/ 
public static void main(String[] args) throws IOException { 

    //Shuffle s = new Shuffle(); 
     // s.shuffle("hello"); 

    String[] words = createListOfWords(); 
    String word = chooseRandomWord(words); 
    String scrambledWord = scrambleWord(word); 
    System.out.println(scrambledWord); 

    /* code for asking userers guess 
    * 
    *use .equals 
    * 
    * 
    */ 

} 
double[] wordList = new double[65573]; 

private static String[] createListOfWords() throws FileNotFoundException, IOException { 
    BufferedReader in = new BufferedReader(new FileReader("wordlist.txt")); 
    String str; 

    List<String> list = new ArrayList<String>(); 
    while ((str = in.readLine()) != null) { 
     list.add(str); 
    } 

    String[] stringArr = list.toArray(new String[0]); 

    // test to see if array stored--System.out.println(Arrays.toString(stringArr)); 
    return stringArr; 

} 

/** 
* 
* @param words 
* @return 
*/ 
private static String chooseRandomWord(String[] words) { 

    int index = new Random().nextInt(words.length); 

    String word = words[index]; 

    return word; 
} 

private static String scrambleWord(String word) { 

    char[] charArray = word.toCharArray(); 



     List<Character> characters = new ArrayList<>(); 

     StringBuilder scrambledWord = new StringBuilder(word.length()); 

      int randPicker = (int) (Math.random() * characters.size()); 
    StringBuilder append = scrambledWord.append(characters.remove(randPicker)); 




/*for (int i = charArray.length - 1; i > 0; i--) { 
int j = ((int)(Math.random())(i + 1)); 

double scrambledWord = charArray[i]; 
charArray[i] = charArray[j]; 
charArray[j] = (char) scrambledWord; 
}*/ 

return scrambledWord.toString() ; 
/*This method should take a word as an argument, and return the 
same word, except scrambled. In order to scramble a word, convert 
it to an array of chars, like this:*/ 
    //char[] charArray = word.toCharArray(); 

} 

    } 
+0

*“代码看起来不错,并建立,但说有错误。”*它说有什么错误? – 2014-11-07 00:19:19

+0

它说在scrambleWord的主要方法有错误,但使用下面的建议解决它,并返回我所需要的。 – 2014-11-07 00:27:09

+0

如果您指的是'IndexOutOfBoundsException'(我必须通过运行代码并创建自己的'wordlist.txt'来重新创建,因为您既没有提供堆栈跟踪也没有提供最小的可编译示例),请查看您的堆栈跟踪。它会告诉你确切的错误在哪里。在你的情况下,它清楚地标识了你对'scrambleWord'中'.remove(...)'的调用,[记录为抛出,如果索引超出范围](http://docs.oracle.com/javase /7/docs/api/java/util/ArrayList.html#remove(int))。请遵循[这些指导原则](http://meta.stackoverflow.com/a/261593/616460)。 – 2014-11-07 00:28:04

回答

0

你从来没有在scrambleWord添加任何字符到您的List。我会使用Collections.shuffle(List)和类似的东西

private static String scrambleWord(String word) { 
    List<Character> characters = new ArrayList<>(); 
    for (char ch : word.toCharArray()) { 
     characters.add(ch); 
    } 
    Collections.shuffle(characters); 
    StringBuilder sb = new StringBuilder(); 
    for (char ch : characters) { 
     sb.append(ch); 
    } 
    return sb.toString(); 
} 
+0

复制粘贴,做完功课,没有解决问题的技巧,另一个充满肚皮的吸血鬼。 – 2014-11-07 00:29:36