定义一个长度为5的int型数组arr,数组元素为1-5(范围包含1和5)之间的任意随机数,且保证5个数不重复
作者:互联网
public static void main(String[] args) {
int[] arr = new int[5];
Random ran = new Random();
for (int i = 0; i < arr.length; i++) {
int random = ran.nextInt(5) + 1;
arr[i] = random;
for (int j = 0; j < i; j++) {
if (arr[i] != arr [j]) {
}else {
i--;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
标签:arr,int,random,++,ran,数组,new 来源: https://blog.csdn.net/a11111ddd/article/details/119360064