产生30个1-40的随机学号,安排一周值日生(不允许重复),输出值日表。用java简单编写
时间: 2024-09-24 20:15:05 浏览: 65
在Java中,我们可以使用`Collections.shuffle()`函数结合数组或列表来生成随机排列的值日生名单,并保证每个学生只出现一次。这里是一个简单的示例代码:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ValueDaySchedule {
public static void main(String[] args) {
// 创建包含1到40随机数的列表
List<Integer> students = new ArrayList<>();
for (int i = 1; i <= 40; i++) {
students.add(i);
}
// 避免重复,使用Set存储,然后转换回List
List<Integer> uniqueStudents = new ArrayList<>(new HashSet<>(students));
// 打乱顺序
Collections.shuffle(uniqueStudents);
// 输出前30个值日生作为一周的值日表
System.out.println("一周值日生表:");
for (int i = 0; i < 30 && i < uniqueStudents.size(); i++) {
System.out.printf("%d号同学值日(第%d天)%n", uniqueStudents.get(i), i+1);
}
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](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)