大家好,我一直在为我的单词益智游戏制作一个测试类,然后输出就是将对象参考号打印到对象上。任何人都有解决方案来打印对象的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);
}
}
答案 0 :(得分:8)
您的WordPuzzleGenerator
课程未覆盖Object
的{{1}}。相反,它包含一个具有不同签名的静态toString
方法。
您需要在toString
课程中使用此签名的方法:
WordPuzzleGenerator
再次看一下,它会使你的@Override
public String toString()
{
...
}
只有静态方法且没有实例成员,因此不清楚你期望WordPuzzleGenerator
返回什么,或换句话说 - 它不清楚是什么{{ 1}}预计会打印。
编辑:
如果您希望toString
打印在构造函数中创建的列表,您应该将它们设为实例成员:
System.out.print(puzzle);
然后你可以像这样覆盖toString:
toString()
答案 1 :(得分:2)
您必须覆盖对象的toString方法,因为您的对象是从java对象继承的
@Override
public String toString(){
\\mystring build up...
return mystring;
注意覆盖注释,这就是诀窍;) 快乐的编码!
答案 2 :(得分:1)
尝试覆盖&#39; toString&#39;你班上的方法如下:
@Override
public String toString()
{
//your code
}