简体   繁体   中英

JAVA PROGRAMMING - unDUPLICATOR error on logic

I was doing this "Random number game" and have come up with the following code...

public void generate()
{
    for(int i=0; arr[i]!=arr[i+1]; i++)
    {
        for(int l=0; l<10; l++)
        {
            Random rdm=new Random();
            arr[l] = rdm.nextInt(range)+1;

        }
        lbtest.setText("Random Numbers: "+arr[0]+"-"+arr[1]+"-"+arr[2]+"-"+arr[3]+"-"+arr[4]+"-"+arr[5]+"-"+arr[6]+"-"+arr[7]+"-"+arr[8]+"-"+arr[9]);
        bgen.setEnabled(false);
        gametext.setText("");
    }


}

I made 3 different levels for this random number game which means the range of random numbers(1-20, 1-30, and 1-50). the 1-30 and 1-50 levels work. but when I click on 1-20, the code doesn't generate any numbers.

I tried debugging the program to see where this code has the error but JCreator doesn't help. It doesn't display the line with the error.

First off, Declare your random variable outside your loops, or else your numbers won't be random at all.

Secondly, the for(int i=0; arr[i]!=arr[i+1]; i++) loop looks very bizarre. if the first 2 members of arr are the same, then the rest of the code you posted won't execute at all. Also, if the last 2 members of arr are not the same, then your code will throw an exception.

The the condition arr[i]!=arr[i+1]; has the potential to produce the bug you describe.

If you elaborate a little bit on your requirements, I might be able to help you further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM