java - School assignment: Generating 7 random numbers, putting them into an array, and making sure each number is unique -


so, have make program have 7 random numbers , have put them array. each number in array cannot same number in array. wrote program this, works of time. when work, works perfectly, , when doesn't work, program runs forever , have force stop it.

i can't seem find mistake. please help!

here code:

package arraysex2;  import java.util.random; import java.util.scanner; public class arraysex2 {       public static void main(string[] args) {         scanner input=new scanner(system.in);         random r=new random();         int random,count=0,count2;         boolean isrepeat=false;         integer[]randoms=new integer[7];          while(count<=randoms.length-1){             random=r.nextint(30)+1;             for(count2=0;count2<count;count2++){                 if(random==randoms[count2]){                     isrepeat=true;                      break;                 }             }             if(!isrepeat){                randoms[count]=random;               count++;              }            }         for(count=0;count<=randoms.length-1;count++){             system.out.print(randoms[count]+" ");         }     }  } 

your problem lies in how handling isrepeat once gets set true. trace after point figure out how while loop terminates. times when works because isrepeat never gets set true.

in general, when loops don't terminate, useful debugging tip @ supposed cause terminate , see why conditions don't change or aren't met.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -