2014-11-21 111 views
0

我被要求创建一个程序,用于存储数组中的一系列适当的名词,形容词和动词。这些必须在程序运行开始时设置。与其询问用户,每次产生字母时,它只是从相应的数组中随机选择单词。数组传递给表示模板的方法。在数组中选择随机字符串以打印消息

我是新来的java,这是我已经设法在下面完成,但是显示错误说void不能转换为打印消息部分的字符串。如果有人能帮助我处理这个我正在努力的简单问题,我会很高兴,我不知道我是否正确地做到了这一点:/。任何帮助,将不胜感激:)

public static void arrays() 
{ 

     final String []noun = {"face", "eyes", "tender", "lips", "ears", "roses"}; 
     Random random = new Random(); 
     int rand1 = random.nextInt(noun.length); 


     final String []verb = {"enchant", "dazzle", "cuddle" , "lure", "desire", "dream" }; 
     Random random2 = new Random(); 
     int rand2 = random2.nextInt(verb.length); 


     final String []adjective = { "Alluring", "Angelic", "Adoring", "Appealing", "Attractive", "beautiful"}; 
     Random random3 = new Random(); 
     int rand3 = random3.nextInt(adjective.length); 


     String message = printmessage (noun[rand1], verb[rand2], adjective[rand3]); 

     JOptionPane.showMessageDialog(null, message); 

} 
    // END arrays 

    public static String printmessage(String r1, String r2, String r3) 
    { 
     String result1; 
     String result2; 
     String result3; 
     String result4; 

     result1 = JOptionPane.showMessageDialog(null, "I would love to " + r2 + " " + r3 + " " + r1 + "\n"); 
     return result1; 
     result2 = JOptionPane.showMessageDialog(null, "Your are my " + r1 + " " + r3 + " " + r2 + "\n"); 
     return result2; 
     result3 = JOptionPane.showMessageDialog(null, "you always look great in that " + r1 + " , as you always do, since your so" + r3 + "\n"); 
     return result3; 
     result4 = JOptionPane.showMessageDialog(null, "I get butterflies when I see you in" + r1 + " , you make me " + r2 + " , in your " + r3 + " world" + "\n"); 
     return result4; 

    } 
+7

嗯,非常浪漫的代码有 – Coffee 2014-11-21 17:06:04

+2

我的猜测是你不是在主要方法中编写代码......而是写在课堂上。显示更多的代码,所以我们可以看到有什么问题.. – user1071777 2014-11-21 17:07:27

+0

这就是说,所有问题都说明了,代码还有什么? @ user1071777 – Arpit 2014-11-21 17:10:34

回答

2

在此行中:

String message = printmessage (rand1, rand2, rand3); 

你应该通过String而是要传递int

改变它是这样的:

String message = printmessage (noun[rand1], verb[rand2], adjective[rand3]); 

编辑:

此外,因为您要创建的字符串message不要删除return但改变方法:

public static String printmessage(String r1, String r2, String r3) 

编辑编辑:

您将需要更新您的printmessage功能实际创建一个String,类似于String printmessage = "I would love to " + r2 + " " + r3 + " " + r1 + "\n"

+0

啊对,我完全按照你的说法做了,它在返回printmessage语句中显示另外1个错误,表示“无法找到符号”,我想如何解决这个问题:/ @Daniel Stanley – Arpit 2014-11-21 17:23:53

+0

@Arpit在返回之前,您仍然需要创建该字符串,请参阅“EDIT EDIT” – 2014-11-21 17:28:26

+0

我改变了它,它提出了更多的错误,说不兼容的类型再次,void不能转换为字符串,@丹尼尔斯坦利 – Arpit 2014-11-21 17:35:54