for (int j = 0; j < num; j++) {
b[j] = (int) Math.random() * (high); //产生1到high之间(包括)的随即数
System.out.println(b[j]);//加上这句你就知道为什么了。
}

解决方案 »

  1.   

    不是哈因为是输出数组
    所以
    ======================
    ottey test=new Lottey();
        int k[]=test.lottey(33,7);
        for(int x=0;x<k.length;x++)
        System.out.println(k[x]);
    ===========================
    应该这样
    而且(int)Math.random()*(high); 
    这样的话永远是0
    应该这样(int)(Math.random()*(high)); 整个程序如下
    ==============================================
    import java.util.*;
    import java.lang.Math;
    public class Lottey
    {
        public int[] lottey(int high,int num)
        {
        int[] a=new int[high]; //high 定义一共有多少个数字
        int[] b=new int[num];  //num定义要从中选几个数字
        for (int i=1;i<=high;i++)
        {
        a[i-1]=i;  //把数组a 从1开始付值
        }
        for(int j=0;j<num;j++)
        {
        b[j]=(int)(Math.random()*(high)); //产生1到high之间(包括)的随即数
       
        }
        Arrays.sort(b);
        return b;
        }
        public static void main(String[] args)
        {
        Lottey test=new Lottey();
        int k[]=test.lottey(33,7);
        for(int x=0;x<k.length;x++)
        System.out.println(k[x]);
       
        }
    }================================
    现在可以正常生成了,可是我如何才能让每个数字只生成一次呢??高手指教啊5555
      

  2.   

    b[j]=(int)(Math.random()*(high)); 加上一个()应该可以
      

  3.   

    你可以这样做:
    1.将生成的随机数wrap到integer类中;
    2.将integer加入到hashset中;
    3.查询set个数,不足循环,等于num时结束
    因为integer已经有了equals方法,所以保证hashset中的数都是不同的
      

  4.   

    感觉你的a[]一直没有用,就用一下吧:for (int i=1;i<=high;i++) {
    a[i-1]=i;  //把数组a 从1开始赋值到high
    }
    for(int j=0;j<num;j++) {
    int k = 0;
    do {
    k = (int)(Math.random()*(high));
    }while(a[k-1] != 0);
    b[j] = k;
    a[k-1] = 0;
    }
      

  5.   

    danceflash的办法不错,感觉就像是个简单的hashmap
      

  6.   

    danceflash(Wine) 感谢哈挖哈哈
     kalolophoenix(炽火了无痕)深奥了些哈哈我才初学~~感激哈
    多谢各位,接贴了!
      

  7.   

    import java.util.*;
    import java.lang.Math;
    public class Lottey
    {
        public int[] lottey(int high,int num)
        {
        int[] a=new int[high]; //high 定义一共有多少个数字
        int[] b=new int[num];  //num定义要从中选几个数字
        int s=0;
        for (int i=1;i<=high;i++)
        {
        a[i-1]=i;  //把数组a 从1开始付值
        //System.out.println(a[i-1]);
        }
        for(int j=0;j<num;j++)
        {
       
        do{
        s=(int)(Math.random()*(high)); //产生1到high之间(包括)的随即数
             }while(a[s-1]!=0);
        b[j]=s;
        a[s-1]=0;
        }
        Arrays.sort(b);
        return b;
        }
        public static void main(String[] args)
        {
        Lottey test=new Lottey();
        int k[]=test.lottey(30,2);
        for(int x=0;x<k.length;x++)
        System.out.println(k[x]);
       
        }
    }
    ===========================================
    不好意思还要麻烦一下,是死循环啊。555
      

  8.   

    b[j]=(int)(Math.random()*(high)); 
    (Math.random())产生的一个浮点数
    将一个float或double值造型成整数值后,总是将小数部分“砍掉”,不作任何进位处理。
      

  9.   

    do{
        s=(int)(Math.random()*(high)); //产生1到high之间(包括)的随即数
             }while(a[s-1]!=0);======================
    while(a[s-1]==0);
    应该这样哈哦呵呵
    可以结了哈