package five; import java.util.*; public class five { public static void main(String [] args) { int myArray[]; myArray=new int[20]; for(int i=0;i<myArray.length;i++) { Random r1=new Random(); int i1=r1.nextInt(29)+1;//0~30之间的随机整数 myArray[i]=i1; System.out.println(i+"\t\t"+myArray[i]); } // for(int i=0;i<myArray.length;i++) // System.out.print(i+" "); // System.out.print("\n"); } }
时间: 2024-01-23 09:03:54 浏览: 72
这是一个Java程序,它创建了一个包含20个元素的整型数组myArray,并使用Random类生成随机整数来填充这个数组。程序首先使用new关键字创建一个长度为20的整型数组myArray。然后,使用for循环遍历数组中的每个元素,并使用Random类的nextInt()方法生成一个大于等于1小于等于30的随机整数,并将其赋值给当前元素。最后,使用System.out.println()方法将数组下标和当前元素的值输出到控制台。
代码中还有一段被注释掉的for循环,它可以用来输出数组的下标,但由于没有被调用,因此不会产生任何输出。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)