调用方法没有得到结果
所以我只是试图打印一个随机的单词,就是这样
Dictionary secret = new Dictionary(); secret.getRandomWord(); System.out.println(secret);
^所有在我的主程序中。 而且我必须使用的是给了我什么
public String getRandomWord(){ Random generator = new Random(); String temp = new String(); temp += dictionary[generator.nextInt(NUMBER_OF_WORDS)]; return temp;
上面的代码是一个给我的类,我必须与之合作。 当我运行我的代码时,我得到program3.Dictionary@a37368
它应该是一个随机的单词。 有任何想法吗?
我想你在找
Dictionary secret = new Dictionary(); String randomWord = secret.getRandomWord(); System.out.println(randomWord);
你现在拥有的是打印secret
引用的Dictionary
对象的toString()
的结果。
编辑
也可以没有额外的变量:
Dictionary secret = new Dictionary(); System.out.println(secret.getRandomWord());
您正在打印Dictionary
对象,尝试存储getRandomWord
方法的返回类型并打印相同的。
String secretStr = secret.getRandomWord(); System.out.println(secretStr);
好吧,我认为你需要改变你的代码。
代替:
Dictionary secret = new Dictionary(); secret.getRandomWord(); System.out.println(secret);
使用:
Dictionary secret = new Dictionary(); String randomWord = secret.getRandomWord(); System.out.println(randomWord);
那么,会发生什么?