日期:2014-05-20 浏览次数:20745 次
public class Test{ private static final int len = 7; public static void main(String[] args) { int[] cp = new int[7]; Set s = new HashSet(); for (int i = 0; i < len; i++) { int a = (int) (Math.random() * 35 + 1); s.add(new Integer(a)); i = s.size(); } for (Object temp : s) { System.out.println((Integer) temp); } } }
------解决方案--------------------
public static void main(String [] args) { List<Integer> list = new ArrayList<Integer>(); for(int i = 0; i < 7; i++){ Integer a = (int)(Math.random()*35+1); if(list.indexOf(a) != -1) {//判断是否与以经生成的随机数相同,相同就重新生成 i--; } else { list.add(a); } } System.out.println(list); }