0

所以我只是想打印一个随机的单词,就是这样

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时,我得到它应该是一个随机词。有任何想法吗?

4

3 回答 3

3

我想你正在寻找

Dictionary secret = new Dictionary();
String randomWord = secret.getRandomWord();        
System.out.println(randomWord);

您当前拥有的是打印.引用toString()的对象的结果。Dictionarysecret


编辑

也可能没有额外的变量:

Dictionary secret = new Dictionary();        
System.out.println(secret.getRandomWord());
于 2013-03-29T01:11:30.420 回答
1

您正在打印Dictionary对象,尝试存储getRandomWord方法的返回类型并打印相同。

String secretStr = secret.getRandomWord();
System.out.println(secretStr);
于 2013-03-29T01:13:01.620 回答
0

好吧,我认为您需要更改代码。

代替:

Dictionary secret = new Dictionary();        
    secret.getRandomWord();        
    System.out.println(secret);

采用:

Dictionary secret = new Dictionary();    
String randomWord = secret.getRandomWord(); 
System.out.println(randomWord); 

那么,发生了什么?

于 2013-03-29T01:38:03.633 回答