选择数组中的随机字符串来打印消息

时间:2014-11-21 17:04:18

标签: java arrays

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

我是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;

    }

1 个答案:

答案 0 :(得分: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"