刚学两天java,想到了一个程序想自己编一下,就是35选7,也就是说买彩票吧。我想用程序写出来,找了半天没找到java随机函数,哪位知道啊?并且随机函数取值每次都是从头随机取。

解决方案 »

  1.   

    Random伪随机另外,彩票不是算出来的
      

  2.   

    我知道不是算出来的。我只是想编个程序让电脑随机的给我选号。
    Random怎么用,说说看
      

  3.   

    Math类里的,看javadoc就知道了
      

  4.   

    i + (int)Math.random() * x 
    i是位移量  x 是范围
      

  5.   

    public class  lesson6
    {
    public static void main(String[] args) 
    {
    for (int j=1;j<8;j++ )
    {
    System.out.println((int)Math.Random()*35);
    }
    }
    }
    报错。
      

  6.   

    static double random()
    注意大小写,不是 Ramdom()====CSDN 小助手 V2.5 2005年11月05日发布====
    CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
    界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
    下载:http://szlawbook.com/csdnv2
      

  7.   

    public class  lesson6
    {
    public static void main(String[] args) 
    {
    for (int j=1;j<8;j++)
    {
    System.out.println((int)Math.random()*35);
    }
    }
    }
    这样的,为什么结果全是0
      

  8.   

    public class  lesson6
    {
        public static void main(String[] args) 
        {
            for (int j=1;j<8;j++)
            {
                System.out.println((int)(Math.random()*35));
            }
        }
    }大哥,先*35再int,不然都int成0了再*35当然还是0了
      

  9.   

    public static void main(String[] args) {
            int nuy;
            for(int i=0;i<35;i++){
                nuy = (int)(Math.random()*35);
                System.out.println(nuy);
            }
        }
      

  10.   

    这样不行,如果我没有记错的话random产升的随机数是[0,1)
    你这样写只能产生[0,34)的随机数,而彩票一般是没有零的
    你可以这样写(36-1)*Math.random()+1
      

  11.   

    public class  lesson6
    {
        public static void main(String[] args) 
        {
    int x[]=new int[6];int y;
            for (int j=0;j<6;j++)
            {
    y=(int)(Math.random()*35);
    for (int i=0;i<6;i++)
    {
    if (y<>x[i])
    x[j]=y;
    }
             }
     for (int i=0;i<6;i++ )
     System.out.println(x[i]);
    }
    }我加了一个不重复的七个数,帮我看看错在哪里啊,我编译时出错。====CSDN 小助手 V2.5 2005年11月05日发布====
    CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
    界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
    下载:http://szlawbook.com/csdnv2
      

  12.   

    Math.random 是产生一个0-1的 Double
      

  13.   

    public class  lesson6
    {
        public static void main(String[] args) 
        {
            int x[]=new int[6];int y;
            for (int j=0;j<6;j++)
            {
       y=(int)((36-1)*Math.random()+1);
       for (int i=0;i<6;i++)  
        {
    if (y<>x[i])  //验证不重复数
    x[j]=y;
        }
             }
    for (i=0;i<6;i++ )
    System.out.println(x[i]);
        }
    }我加了一个不重复的七个数,帮我看看错在哪里啊,我编译时出错。
      

  14.   

    <>错了,应该是!=;
    只能出6个随机数,不是7个;
    大概看一下,无法避免重复;
    楼主继续加油 :)
      

  15.   

    if(y<>x[i])  是什么东西
      

  16.   

    呵呵,谢谢,是我自己搞错了。我搞成Qbese语言里面的比较符了。
    仔细看了我的程序,明白了还是不能避免重复,下班了,明天再写。
    谢谢你们。
      

  17.   

    public class  lesson6
    {
        public static void main(String[] args) 
        {
    int x[]=new int[6];int y;
            for (int i=0;i<7;i++)
    {
    y=(int)((36-1)*Math.random()+1);
    x[i]=y;
    for (int j=0;j<i;j++)
    {
    if (x[i]==x[j])
    {
    i=i--;
    break;
    }
    }
    }
    for (int a=0;a<7;a++)
    {
    System.out.println(x[a]);
    }
    }
    }
    这样不知道对不对,为什么可以编译,但运行时出错??
      

  18.   

    这个不知道行不行
    public class lesson6{
       public static void main(String[] args){
         int r=0;
         int[] y=new int[35];
         boolean b=new boolean[35];
         for(int i=0;i<35;i++){
           a[0]=i+1;
         
         }
         for(int j=0;j<7;j++){
           do{
             r=int(35*Math.random());
             
             }
           while(b[r]=false);
           b[r]=true;
           System.out.println(y[r]);
              
         
      }
    }
      

  19.   

    public class  lesson6
    {
        public static void main(String[] args) 
        {
    int x[]=new int[6];int y;
            for (int i=0;i<7;i++)  //这里的i已经越界了,你的数组声明为6,你这里不应该这样处理,可修改为i<x.length;
    {
    y=(int)((36-1)*Math.random()+1);
    x[i]=y;
    for (int j=0;j<i;j++)
    {
    if (x[i]==x[j])
    {
    i=i--;
    break;
    }
    }
    }
    for (int a=0;a<7;a++)
    {
    System.out.println(x[a]);
    }
    }
    }
      

  20.   

    public class  lesson6
    {
    public static void main(String[] args) 
    {
    for (int j=1;j<=8;j++ )
    {
    System.out.println((int)(Math.random()*35)); }
    }
    }
    运行成功!!!!!
      

  21.   

    谢谢你们,但是我还是没解决。
    james1108(阿酷拉)兄:
    我运行时报错为lesson6.java:12:'.class' expected
    r=int(35*Math.random());            
    1 error
    后来我改为r=(int)(35*Math.random());   报错更多了:
    lesson6.java:5: incompatible types
    found   : boolean[]
    required: boolean
         boolean b=new boolean[35];
                   ^
    lesson7.java:7: cannot resolve symbol
    symbol  : variable a
    location: class lesson7
           a[0]=i+1;
           ^
    lesson7.java:15: array required, but boolean found
           while(b[r]=false);
                  ^
    lesson7.java:16: array required, but boolean found
           b[r]=true;
            ^
    4 errors
      

  22.   

    peacedog()兄:
    我编译时没出错,但是执行时就如下:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
            at lesson6.main(lesson6.java:9)
      

  23.   

    liaojinan()兄:
    你的程序是出现在35以内的7个随机数但没有保证不重复,我现在想实现35以内的7个不重复随机数
      

  24.   

    Random  random = new Random();
        public int GetRandom ( int nMin, int nMax )
        {
    int iRet = random.nextInt() ;
    if ( iRet < 0 )
    {
        iRet = - iRet ;
    }
    iRet = iRet % ( nMax + 1 - nMin ) + nMin;
    return iRet;
        }
      

  25.   

    对不起,^^
    boolean[] b=new boolean[35];