日期:2014-05-20 浏览次数:20701 次
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
public class RandomNumber {
public static Set<Integer> GetRandomNumber() {
// 使用SET以此保证写入的数据不重复
Set<Integer> set = new HashSet<Integer>();
Random random = new Random();
while (set.size() < 5) {
set.add(random.nextInt(10));
}
return set;
}
public static void main(String[] args) {
Set<Integer> set = GetRandomNumber();
Iterator<Integer> iterator = set.iterator();
String temp = "";
while (iterator.hasNext()) {
temp += iterator.next();
}
System.out.println(temp);
}
}