-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray of 6
More file actions
36 lines (35 loc) · 1.03 KB
/
Copy patharray of 6
File metadata and controls
36 lines (35 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Random;
/**
* 创建一个含有6个元素的一维数组,随机赋值,保证每个值都不一样
*/
public class random {
public static void main(String[] args)
{
int arr[]=new int[6];
Random random=new Random();
arr[0]=Math.abs(random.nextInt()%10);
for (int i=1;i<6;i++)
{
arr[i]=Math.abs(random.nextInt()%10);
int now=i-1; //与前面的数值一一比较
while (true)
{
if (arr[i]==arr[now])
{
arr[i]=Math.abs(random.nextInt()%10); //与前面的数值相同,重新赋值
continue;
}
else //继续往前比较
{
now--;
if (now<0) //已经比较到第一个元素
break;
continue;
}
}
}
// 输出数组
for (int e:arr)
System.out.println(e);
}
}