Java-Random 从五个人中随机三个 且不能重复
package demo;
import java.util.Random;
/**
* 随机不重复的三个人
*
* @author Weirdo-world
*
*/
public class Demo8 {
public static void main(String[] args) {
String[] s = { “张三”, “李四”, “王五”, “赵虎”, “周天” };
Random r = new Random();
String[] st = { “”, “”, “” };
for (int i = 0; i < 3; i++) {
String str = s[r.nextInt(5)];
for (int j = 0; j < st.length; j++) {
while (str.equals(st[j])) {
j = 0;
str = s[r.nextInt(5)];
}
}
st[i] = str;
}
for (int i = 0; i < st.length; i++) {
System.out.println(st[i]);
}
}
}