package test;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Snum {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建一个Listrj列表里面存放Integer数据。
List<Integer>listrj=new ArrayList<Integer>();
/*建立一个随机对象r1,while循环,
其中contains是包含的意思,字符串“abc”contains("a"),
返回结果boolean类型。*/
Random r1 = new Random();
while (true) {
int num = r1.nextInt(9) + 1;
if (!listrj.contains(num))
listrj.add(num);
if (listrj.size() >= 5)
break;
}
//for循环遍历输出
for(Integer a :listrj ) {
System.out.println(a);
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Snum {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建一个Listrj列表里面存放Integer数据。
List<Integer>listrj=new ArrayList<Integer>();
/*建立一个随机对象r1,while循环,
其中contains是包含的意思,字符串“abc”contains("a"),
返回结果boolean类型。*/
Random r1 = new Random();
while (true) {
int num = r1.nextInt(9) + 1;
if (!listrj.contains(num))
listrj.add(num);
if (listrj.size() >= 5)
break;
}
//for循环遍历输出
for(Integer a :listrj ) {
System.out.println(a);
}
}
}