Java-Random 從五個人中隨機三個 且不能重複
阿新 • • 發佈:2019-01-01
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]);
}
}
}