gpt4 book ai didi

java - 我可以将一个数组存储到另一个数组中吗?

转载 作者:行者123 更新时间:2023-12-01 09:52:55 24 4
gpt4 key购买 nike

我正在尝试用java编写一个程序,其中我创建了三个数组,一个用于男孩,其中包含男孩的名字,一个用于表达感情(爱,恨,崇拜),第三个用于女孩,其中包含名字的女孩。

现在我为每个数组生成随机数,并将它们连接起来以给出类似 ...(boys[]+""+feelings[]+""+girls[]) 的输出,例如 Sagar 讨厌 Sakshi

现在这句话是关于男孩的。我想生成另一个关于像 Sakshi Love Shubham 这样的女孩的字符串。现在这些新生成的字符串也应该随机打印。,我该怎么做?

我的源代码是这样的...

public class Random
{
public static void main(String args[])
{
String[] boys={"shivam","shubham","sagar","Tushar","tarun"}; String[]=girls{"payal","preeti","neetika","sakshi_jain","sakshi_singh","wafa","Reshu","Pragya"};
String[] feelings={"Love","Hate","Adore","want_to_kill"};

int x=boys.length;
int y=girls.length;
int z=feelings.length;

for(int i=0;i<10;i++)
{
int rand1=(int)(Math.random()*x);
int rand2=(int)(Math.random()*y);
int rand3=(int)(Math.random()*z);

String phase1=girls[rand2]+" "+feelings[rand3]+" "+boys[rand1];
String phase2=boys[rand1]+" "+feelings[rand3]+" "+girls+[rand2];
String[][] phase={phase1,phase2};

int p=phase.length;
int q=(int)(Math.random()*p);

System.out.println(phase[0][q]);
}
}
}

最佳答案

请您检查以下解决方案:

public class Random {
public static void main(String args[]) {
String[] boys = { "shivam", "shubham", "sagar", "Tushar", "tarun" };
String[] girls = { "payal", "preeti", "neetika", "sakshi_jain", "sakshi_singh", "wafa", "Reshu", "Pragya" };
String[] feelings = { "Love", "Hate", "Adore", "want_to_kill" };

String[][] emotions = new String[2][];
emotions[0] = new String[boys.length];
emotions[1] = new String[girls.length];

// boys emotions
for (int i = 0; i < boys.length; i++) {
String subject = boys[i];
String verb = getRandomItem(feelings);
String object = getRandomItem(girls);
emotions[0][i] = subject + " " + verb + " " + object;
}

// girls emotions
for (int i = 0; i < girls.length; i++) {
String subject = girls[i];
String verb = getRandomItem(feelings);
String object = getRandomItem(boys);
emotions[1][i] = subject + " " + verb + " " + object;
}

// printing two dimensional array
for (int i = 0; i < emotions.length; i++) {
for (int j = 0; j < emotions[i].length; j++) {
System.out.println(emotions[i][j]);
}
}

}

// returns a random element
private static String getRandomItem(String[] arr) {
int max = arr.length - 1;
int min = 0;
java.util.Random random = new java.util.Random();
int index = random.nextInt(max - min + 1) + min;
return arr[index];

}
}

告诉我结果?

关于java - 我可以将一个数组存储到另一个数组中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37494958/

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