随机生成10个数,赋给一维数组 ,然后遍历并且改写成一个方法java
时间: 2024-05-21 10:11:07 浏览: 12
public class RandomArray {
public static void main(String[] args) {
int[] arr = generateRandomArray(10);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static int[] generateRandomArray(int length) {
int[] arr = new int[length];
for (int i = 0; i < length; i++) {
arr[i] = (int)(Math.random() * 100);
}
return arr;
}
}
相关问题
随机生成10个数,赋给一维数组 ,改写成一个方法java
public static int[] generateRandomArray(int size) {
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = (int) (Math.random() * 100);
}
return arr;
}
// 调用方法生成长度为10的随机数组
int[] arr = generateRandomArray(10);
随机生成十个数赋给一维数组
以下是Python代码示例:
```python
import random
# 生成随机数并存入数组
arr = []
for i in range(10):
num = random.randint(1, 100)
arr.append(num)
# 输出数组
print(arr)
```
运行结果可能如下:
```
[63, 17, 51, 85, 89, 91, 73, 18, 27, 62]
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](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)