嗨,大家好,我一直在为我的单词益智游戏做一个测试班,结果是将对象参考号打印到对象上。任何人都有解决方案可以打印对象的return语句。
输出:
生成器统计信息:从长度为3的单词生成的单词拼图
Puzzle.WordPuzzleGenerator @ c68c3Puzzle.WordPuzzleGenerator @ b2002fPuzzle.WordPuzzleGenerator @ 2a4983Puzzle.WordPuzzleGenerator @ 406199Puzzle.WordPuzzleGenerator @ c7b00cPuzzle.WordPuzzleGenerator @ 1f6f296Puzzle.WordPuzzleGenerator @ 1df5a8fPuzzle.WordPuzzleGenerator @ b2a2d8Puzzle.WordPuzzleGenerator @ 1e13d52Puzzle.WordPuzzleGenerator @ 80fa6f
测试班
public class Test_WordPuzzleGenerator {
public static void main(String[] args) throws FileNotFoundException {
int sizeTest1 = 3;
System.out
.println("Generator stats: word-puzzles generated from words of length "
+ sizeTest1);
for (int i = 0; i < 10; i++) {
WordPuzzleGenerator puzzle = new WordPuzzleGenerator(sizeTest1);
System.out.print(puzzle);
}
int sizeTest2 = 3;
System.out
.println("Generator stats: word-puzzles generated from words of length "
+ sizeTest2);
for (int i = 0; i < 10; i++) {
new WordPuzzleGenerator(sizeTest2);
}
}
}
主程序:
public class WordPuzzleGenerator {
static ArrayList<String> wordList = new ArrayList<String>();
public WordPuzzleGenerator(int size) throws FileNotFoundException {
ArrayList<String> puzzleListY = new ArrayList<String>();
ArrayList<String> puzzleListX = new ArrayList<String>();
String randomXWord;
String letterSize = "" + size;
makeLetterWordList(letterSize);
boolean finished = false;
while ( !finished ) {
finished = true;
puzzleListX.clear();
puzzleListY.clear();
for (int i = 0; i < size; i++) {
int randomYWord = randomInteger(wordList.size());
String item = wordList.get(randomYWord);
puzzleListY.add(item);
}
for (int i = 0; i < puzzleListY.size(); i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < puzzleListY.size(); j++) {
sb.append(puzzleListY.get(j).charAt(i));
}
randomXWord = sb.toString();
if (!wordList.contains(randomXWord) && !puzzleListY.contains(randomXWord)) {
finished = false;
break;
}
puzzleListX.add(randomXWord);
}
}
toString(puzzleListX, puzzleListY);
}
public static int randomInteger(int size) {
Random rand = new Random();
int randomNum = rand.nextInt(size);
return randomNum;
}
public static void makeLetterWordList(String letterSize) throws FileNotFoundException {
Scanner letterScanner = new Scanner( new File (letterSize + "LetterWords.txt"));
wordList.clear();
while (letterScanner.hasNext()){
wordList.add(letterScanner.next());
}
letterScanner.close();
}
public static String toString(ArrayList<String> ArrayList1, ArrayList<String> ArrayList2){
StringBuilder group1 = new StringBuilder();
for (int i = 0; i < ArrayList1.size(); i++) {
group1.append(ArrayList1.get(i) + " ");
}
String wordsInString1 = group1.toString();
StringBuilder group2 = new StringBuilder();
for (int i = 0; i < ArrayList2.size(); i++) {
group2.append(ArrayList2.get(i) + " ");
}
String wordsInString2 = group2.toString();
return String.format("\t( %s) ( %s)", wordsInString1, wordsInString2);
}
}
最佳答案
您的WordPuzzleGenerator
类不会覆盖Object
的toString
。相反,它包含带有不同签名的静态toString
方法。
您需要在WordPuzzleGenerator
类中使用此签名的方法:
@Override
public String toString()
{
...
}
再看一遍,它使您的
WordPuzzleGenerator
仅具有静态方法,没有实例成员,因此尚不清楚您期望toString
返回什么,或者换句话说-您不清楚要输出什么System.out.print(puzzle);
。编辑:
如果要
toString()
打印在构造函数中创建的列表,则应使它们成为实例成员:ArrayList<String> puzzleListY;
ArrayList<String> puzzleListX;
public WordPuzzleGenerator(int size) throws FileNotFoundException {
puzzleListY = new ArrayList<String>();
puzzleListX = new ArrayList<String>();
...
}
然后,您可以像这样重写toString:
@Override
public String toString()
{
return WordPuzzleGenerator.toString (puzzleListX,puzzleListY);
}