调用方法不获得结果 [英] calling a method not getting result

查看:139
本文介绍了调用方法不获得结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我只是打印一个随机单词和它

so im just trying to print a random word and thats it

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

^所有在我的主程序。
给我的,我必须使用

^all that is in my main program. And what was given to me that i have to use

 public String getRandomWord(){
    Random generator = new Random();
    String temp = new String();
    temp += dictionary[generator.nextInt(NUMBER_OF_WORDS)];
    return temp;

上面的代码是给我的一个类,我必须使用。当我运行我的代码,我得到 program3.Dictionary@a37368 当它应该是一个随机词。任何想法?

the above code is a class that was given to me that i have to work with. when i run my code i get program3.Dictionary@a37368 when it should be a random word. Any ideas?

推荐答案

我想你正在寻找

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

您目前拥有的是打印 toString() 引用的字典对象。

What you currently have is printing the result of toString() of the Dictionary object referenced by secret.

EDIT

也可以没有额外的变量:

Also possible without an extra variable:

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

这篇关于调用方法不获得结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆