⽣成三个字符串类型的数组,第⼀个数组中⼿动存⼊代表名字的字符串10个,第⼆个数 组存⼊代表地点的字符串15个,第三个数组存⼊代表⾏为的字符串12个,使⽤⽣成随 机数的⽅法Math.random()来随机拼接句⼦。
时间: 2024-03-26 14:36:06 浏览: 13
好的,根据您的要求,我可以提供以下代码示例:
```java
public class RandomSentenceGenerator {
public static void main(String[] args) {
String[] names = {"Alice", "Bob", "Charlie", "David", "Eve", "Frank", "Grace", "Henry", "Ivy", "John"};
String[] places = {"beach", "forest", "mountain", "city", "park", "river", "desert", "island", "cave", "valley", "ocean", "lake", "castle", "temple", "space"};
String[] actions = {"ran", "jumped", "climbed", "swam", "flew", "slept", "laughed", "sang", "danced", "ate", "drank", "read"};
int numSentences = 5;
for (int i = 0; i < numSentences; i++) {
String name = names[(int) (Math.random() * names.length)];
String place = places[(int) (Math.random() * places.length)];
String action = actions[(int) (Math.random() * actions.length)];
String sentence = name + " " + action + " at the " + place + ".";
System.out.println(sentence);
}
}
}
```
这个程序会随机从名字、地点和行为的数组中选择一个元素,然后拼接成一个句子。程序会生成5个这样的句子,并输出到控制台上。您可以根据需要修改数组的大小和生成的句子数量。