gpt4 book ai didi

java - 尝试打印 StringBuilder 类型 : "com.jeanlucthumm.poem.Word@7852e922" 时出现奇怪的输出

转载 作者:太空宇宙 更新时间:2023-11-04 06:42:34 25 4
gpt4 key购买 nike

我的程序旨在通过重复打印随机生成的行来创建一首随机诗。我有一个名为 Line 的类,它有一个可操作的场线:

private StringBuilder line = new StringBuilder();

构造函数如下所示:

public Line(int length, String pathOfWordList) throws IOException {
this.length = length;
populateLine(length, pathOfWordList);
}

单词列表包含三种类型的单词:名词、动词和形容词,每种单词都有被选择的概率。

populateLine 选择并准备一个单词以添加到 StringBuilder 行中。 Words 是 Word 类的对象,有两个字段:

private Type wordType;
private String word;

其中 Type 是具有三种单词类型的枚举。

填充行然后通过调用调用方法的方法来添加单词。第一个方法具有以下签名:

// currentWord is the word that we have to insert after.
// wordList is the word bank we draw from.
// line is the line we are working with.
// The last two doubles are the probabilities of the three types of words.
// The third one can be inferred
private void getNextWord(Word currentWord, WordList wordList,
StringBuilder line, double nounProb, double verbProb)

该方法有一堆调用该方法的循环:

// Adds a word to the line and updates its type
// Used by getNextWord
private void loopHelper(Word currentWord, Type type, WordList wordList,
StringBuilder line) {
currentWord.setType(type);
currentWord.setWord(wordList.getWord(type));
line.append(" " + currentWord);
}

最后出于测试目的,我制作了一个打印出该行的方法:

public void printPoemLine() {
System.out.println(this.line.toString());
}

但是当我实例化并调用该方法时,我得到了这个奇怪的输出:

com.jeanlucthumm.poem.Word@7852e922 com.jeanlucthumm.poem.Word@7852e922 com.jeanlucthumm.poem.Word@7852e922 com.jeanlucthumm.poem.Word@7852e922 com.jeanlucthumm.poem.Word@7852e922 com.jeanlucthumm.poem.Word@7852e922

谁能告诉我那是什么?我只在互联网上找到了一篇具有此类输出的文章,并且它正在处理类型删除,但我不确定这是否适用于此处。

最佳答案

通过这样做:

line.append(" " + currentWord);

您正在隐式调用 currentWord.toString()默认情况下,它返回一个如下所示的字符串: com.jeanlucthumm.poem.Word@7852e922
这是 <the name of the class>@<hash-code of the object>

关于java - 尝试打印 StringBuilder 类型 : "com.jeanlucthumm.poem.Word@7852e922" 时出现奇怪的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24480430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com