用输入语句输入变量种子seed,基于seed创建随机对象,生成10个[0,100)之间的随机数,存到数组中。要求打印输出数组元素,每两个数组元素之间用空格隔开,最后一个元素后无空格。
时间: 2024-03-06 12:46:21 浏览: 102
可以使用Scanner类来获取用户输入的种子seed,然后基于seed创建Random对象,生成随机数并存储到数组中,最后打印输出数组元素。代码如下:
```java
import java.util.Random;
import java.util.Scanner;
public class RandomArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入种子:");
int seed = sc.nextInt();
Random random = new Random(seed); // 基于seed创建随机对象
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextInt(100); // 生成[0,100)之间的随机数
}
for (int i = 0; i < arr.length; i++) {
if (i != arr.length - 1) {
System.out.print(arr[i] + " "); // 输出数组元素并用空格隔开
} else {
System.out.print(arr[i]); // 输出最后一个数组元素,后面无空格
}
}
}
}
```
运行结果示例:
```
请输入种子:666
51 81 1 95 71 55 69 87 88 92
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)
![](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)