先看看我的一个类,很短,耐心看看哦。
我是希望从这个类中得到四个不同的0-9的数,但是编译运行后,有时候能得到,有时候又得不到,不知道到底哪里出错了,请各位高手指点。另外如果有更好的方法得到四个不同的0-9的数的方法还望不吝赐教!!!先谢谢各位咯!!class begin implements Runnable
{
int a[]=new int[4];
int p=0;
public void run()
{ for(int i=0;i<4;i++)

{
a[i]=(int)(Math.random()*9)+1;
while(p<i)
{

for(int j=0;j<i;j++)
{
if(a[i]==a[j])
{
a[i]=(int)(Math.random()*9)+1;
p=0;
break;
}
else
{
p++;
}

}
}
}
}}

解决方案 »

  1.   

    上面的while(p<i)改成while(p!=i)理论上说是可以的,但是我改后却得不到正确的数,不改又会出现重复。我对这个代码分析后,认为
    if(a[i]==a[j])
    {
    a[i]=(int)(Math.random()*9)+1;
    p=0;
    break;
    }
    这里的p=0这句没有起作用,我在另外一个class里面的main方法中运行时候调用这里的p,发现p的值好大啊。
      

  2.   

    我也试过把run方法当作普通方法调用,但是也一样。有没有个厉害的来告诉我啊
      

  3.   

    在WHILE 循环结束后应该加上p=0;
    public class first 
    {
            public static int a[]=new int[4];
            int p=0;
            public void run()
            {                for(int i=0;i<4;i++)                {
                            a[i]=(int)(Math.random()*9)+1;
                            while(p<i)
                            {                                for(int j=0;j<i;j++)
                                    {
                                            if(a[i]==a[j])
                                            {
                                                    a[i]=(int)(Math.random()*9)+1;
                                                    p=0;
                                                    break;
                                            }
                                            else
                                            {
                                                    p++;
                                            }                                }
                                    
                            }
                            p=0;
                    }
            }
            public static void main(String args[])
            {
                new first().run();
                for(int i=0;i<4;i++)
                {
                    System.out.println(a[i]);
                }
            }}
      

  4.   

    我还是写你的那个类比较好class begin implements Runnable
    {
    int a[]=new int[4];
    int p=0;
    public void run()
    { for(int i=0;i<4;i++)

    {
    a[i]=(int)(Math.random()*9)+1;
    while(p<i)
    {

    for(int j=0;j<i;j++)
    {
    if(a[i]==a[j])
    {
    a[i]=(int)(Math.random()*9)+1;
    p=0;
    break;
    }
    else
    {
    p++;
    }

    }//for
    }//while
                       p=0;//关键
    }//for
    }//void run()}//class
      

  5.   

    //把p放在循环里边就OK了,每成功取得一个数字,就要给p清0    
    for (int i = 0; i < 4; i++)
        {
          int p = 0;
          a[i] = (int) (Math.random() * 9) + 1;
          while (p < i) {
            for (int j = 0; j < i; j++) {
              if (a[i] == a[j]) {
                a[i] = (int) (Math.random() * 9) + 1;
                p = 0;
                break;
              }
              else {
                p++;
              }
            }
          }
        }
        for(int i=0;i<4;i++){
          System.out.println(a[i]);
        }
      

  6.   

    我喜欢用ArrayList实现,用一个布尔变量来替代上面的p变量,这样似乎可读性好点
    另外(int) (Math.random() * 9) + 1产生的随机数是[1,9]而不是楼主要求的[0,9]
    可以用java.util.Random.nextInt(int)方法来替代
     
    import java.util.*;
    public class begin implements Runnable
    {
    static ArrayList array = new ArrayList(); 
    public void run()
    {
    Random rand = new Random();
    boolean success = false;
    for(int i=0;i<4;i++)
    {
    success = false;
    Integer integer = new Integer(rand.nextInt(10));
    while(!success)
    {
    if(!array.contains(integer))
    {
    success = true;
    array.add(integer);
    }
    else
    integer = new Integer(rand.nextInt(10));
    }
    }
    }

    public static void main(String[] args)
    {
    new begin().run();
    System.out.println(array);
    }
    }
      

  7.   

    谢谢各位的热情答复,我刚刚又看了自己的程序(可能是第100多遍了);终于发现了在while循环后没有结束p的值,写上去后还真运行得了了。郁闷了我3天。刚刚这里网络通了,然后登陆上来看有没有更好的方法,呵呵。谢谢大家咯。马上结贴。