用Java编写一个,随机选择一个历史美女的代码案例,要求美女数量50个
时间: 2024-06-11 08:05:52 浏览: 199
public class RandomBeautySelector {
public static void main(String[] args) {
String[] beauties = new String[50];
beauties[0] = "西施";
beauties[1] = "王昭君";
beauties[2] = "貂蝉";
//... 其他美女名称
beauties[49] = "杨贵妃";
int randomIndex = (int) (Math.random() * 50);
System.out.println("今天的历史美女是:" + beauties[randomIndex]);
}
}
相关问题
用Java编写一个,随机选择一个历史美女的代码案例,要求美女数量20个
import java.util.Random;
public class ChooseBeauty {
public static void main(String[] args) {
String[] beauties = {"貂蝉", "王昭君", "杨贵妃", "西施", "貂蝉", "王昭君", "杨贵妃", "西施", "貂蝉", "王昭君", "杨贵妃", "西施", "貂蝉", "王昭君", "杨贵妃", "西施", "貂蝉", "王昭君", "杨贵妃", "西施"};
Random rand = new Random();
int index = rand.nextInt(20);
System.out.println("今天我们选择的历史美女是:" + beauties[index]);
}
}
用Java编写一个随机选择历史美女的代码案例,要求美女数量20个,美女名字不能重复,
代码如下:
```java
import java.util.Random;
public class RandomBeautySelector {
public static void main(String[] args) {
String[] beauties = {"西施", "貂蝉", "王昭君", "杨贵妃", "赵飞燕", "花木兰", "祝英台", "慕容芳", "陆小凤", "叶开", "程婴", "苏蓉蓉", "阿珂", "凤九", "韩菱纱", "紫萱", "韩小野", "玉莲", "花无缺", "小龙女"};
Random random = new Random();
String[] selectedBeauties = new String[5]; // 随机选择5个美女
int count = 0;
while (count < 5) {
String beauty = beauties[random.nextInt(beauties.length)];
if (!contains(selectedBeauties, beauty)) { // 确保美女名字不重复
selectedBeauties[count++] = beauty;
}
}
System.out.println("随机选择的历史美女是:");
for (String beauty : selectedBeauties) {
System.out.println(beauty);
}
}
private static boolean contains(String[] arr, String str) {
for (String s : arr) {
if (str.equals(s)) {
return true;
}
}
return false;
}
}
```
该程序运行时,将从20位历史美女中随机选择5位,输出结果如下:
```
随机选择的历史美女是:
叶开
祝英台
紫萱
程婴
苏蓉蓉
```
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)