gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:14:40 24 4
gpt4 key购买 nike

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

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

}

最佳答案

在这一行:

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 like to "+ r2 + ""+ r3 + “” + r1 + “\n”

关于java - 选择数组中的随机字符串来打印消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27066603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com