随机产生7个数  但不能重复  应该怎么做哦   就要那六合彩一样!!!  老师出的题目  唉    

解决方案 »

  1.   

    提示: 用Random类,数组。作业都要别人做。汗。
      

  2.   

    import java.util.HashSet;
    import java.util.Set;public class Random7 {
    public static void main(String[] args) {
    Set<Integer> results = new HashSet<Integer>();
    int domain = 10000;
    while (results.size() < 7){
    results.add(new Integer((int)(Math.random()*domain)));
    }
    for (int i : results) System.out.println(i);
    }
    }
      

  3.   

      hehe   老师说了不会做的话  就 问电脑的  呵呵   所以就求救各位高手
      

  4.   

    楼主很直白,鼓励诚实,但以后要努力学习,为中国崛起而读书!!!import java.util.*;public class Test {
        public static void main(String[] args) {
            // rand用来产生随机数
            Random rand = new Random(System.currentTimeMillis());
            
            final int len = 7; // 要产生的随机数个数
            int index = 0; 
            int[] numbers = new int[len]; // 存储产生的随机数
            
            do {
                int n = rand.nextInt(1000);
                if (!Test.isIn(numbers, n)) {
                    // 如果新产生的随机数不在数组里,加入数组里.
                    numbers[index++] = n;
                }
            } while (index < len);
            
            System.out.println(Arrays.toString(numbers));
        }
        
        // 判断一个数是否已经在指定的数组中,如在,返回true, 不在返回false.
        public static boolean isIn(int[] ns, int n) {
            for (int i = 0; i < ns.length; ++i) {
                if (ns[i] == n) {
                    return true;
                }
            }
            
            return false;
        }
    }
      

  5.   

    你个意思只能用数组咯。3楼上那个用的是集合,Set是一个不可以重复的集合。我帮你下个用数组的实现。测试正确,版主回去看看。
    import java.util.Random;public class Test2 { public static void main(String args[]) {
    int[] a = new int[7];
    boolean isSame = false;
    int count = 0;
    Random r = new Random();
    while(count < 7) {
    isSame = false;
    int t = r.nextInt(10);
    for(int i = 0; i < count; i ++) {
    if(a[i] == t) {
    isSame = true;
    }
    }
    if(!isSame) {
    a[count] = t;
    count ++;
    }
    }

    for(int s : a) {
    System.out.println(s);
    }
    }
    }
      

  6.   


     这不就是和彩票一样了么,从33个中选7个。这7个不重复就行是吧?如果是这样参照我http://topic.csdn.net/u/20100724/17/9d6537ab-0b17-470a-9205-6a3085e2dc60.html?75403这个帖子的回答!
      

  7.   

     heh e   各位谢了!! 现在能看懂了
      

  8.   

    for(int s : a) {
    System.out.println(s);
    }
    }
    }这S:a 是什么意思啊
      

  9.   

    for(int s : a) {
    System.out.println(s);
    }
    }
    }
    这是个遍历 就是将数组a种的所有元素都输出
    就相当于
    for(int x = 0;x < a.length;a++){
    System.out.println(a[x]);
    }
      

  10.   

    public class haha {

    public static List<Integer> list = new ArrayList<Integer>();

    public static void main(String[] args) throws ParseException {
    for (int i = 0; i < 7; i++) {
    list.add(new Integer((int)(Math.random()*10)));
    }
    for (int i = 0; i < list.size(); i++) {
    System.out.print(list.get(i) + "  ");
    }
    }}
      

  11.   

    最权威的一个  哈哈  绝对正确 import java.util.HashSet;
    import java.util.Set;
    public class haha {

    public static Set<Integer> list = new HashSet<Integer>();

    public static void main(String[] args) throws Exception {
    for (int i = 0; i < 7; i++) {
    list.add(new Integer((int)(Math.random()*1000)));
    }
    for (int i : list) {
    System.out.print(i + "  ");
    }
    }}
      

  12.   

    真逗啊,如楼上各位所说用Random类,生成的数字与现有的一一比对就好了,当年上学的时候用C打印一个三角形都老费劲了
      

  13.   

    public static void main(String[] args) {
    int[] arr = new int[7];
    int[] nums = new int[10];
    Random r = new Random();
    for (int i = 0; i < 10; i++)
    nums[i] = i;
    for (int i = 0; i < 7; i++) {
    int j = 0;
    while ((nums[j = r.nextInt(10)]) == 0)
    ;
    arr[i] = nums[j];
    nums[j] = 0;
    }
    System.out.println(Arrays.toString(arr));
    }
      

  14.   

    9L的方法可以解决问题,但是每次随机到一个已经被挑选过的数据,这次随机就会失效并重新随机,这个过程未免有点浪费了。这么说吧,如果随机的数据个数占总数的百分比也大,这个方法的效率也就也低。假如有0--1000个数据,分别从中随机500,700,900个不重复的数据,对9L的方法做了简单的测试如下:
               随机个数/总数         500/1000         700/1000     900/1000
       调用nextInt的平均随机次数       693               1203        2308当然对于从1000个数据里面随机900个数,可以反过来算随机100个,然后在从总数里面减去。我有一个办法,随机次数与需要随机的数据量相等:public class Test {
    public static int[] produce(int[] array,int num){
    int[] result =new int[num];
    int arrLen=array.length;
    Random rand = new Random(System.currentTimeMillis());
    for(int i=0;i<num;i++){
    int pos=rand.nextInt(arrLen-i); //随机一个位置
    result[i]=array[pos];
    array[pos]=array[arrLen-i-1]; //把该位置上的数据交换掉
    }

    return result;
    }

    public static void main(String[] args){
    int[] a=new int[1000];
    for(int i=0;i<1000;i++)
    a[i]=i+1;
    int[] produced=produce(a,8);
    for(int p:produced)
    System.out.println(p);
    }
    }
      

  15.   

    int [] cai= new int[7];
    int [] user = new int[7];
    int i , j , count;
    i = 0;

    do
    {
    cai[i] = (int)(Math.random() * 35 + 1);   //产生一个1-35随机数存入数组
    //查询此数是否已经出现在前面的元素中
    find = false;
    for(j = 0; j < i; j++)
    {
    if(cai[i] == cai[j])
    {
    find = true;
    break;
    }
    }
    if(find == false)  //如果没有出现,则继续下一个数
    i++;

    if(i == cai.length)  //产生了7个数后即可退出循环
    break;

    }while(true);