#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Continue to Outer Loop Isn't Working


    Hey everyone. So my homework is to run a lottery program with a winning list picked randomly that doesn't have duplicates. That's the part I'm having problems with. I'm trying to check for duplicates and re-run the assignment loop if there is a duplicate. But everything I've tried, including continuing to the outer loop which I thought should work, generally creates a permanent loop. Help would be appreciated. Code segment is below.

    Code:
    int [] winners = new int[5];
    		int c=0;
    		
    		outer: while (c<5)
    		{
    			Die d1 = new Die(1, 10);
    			int w= d1.roll();
    			winners[c]=w;
    			System.out.print(" "+winners[c]);
    				
    			for(int i=0;i<5;i++)
    			{ 
    				boolean dup = false;
    				
    				for(int z=0;z<5;z++)
    				{
    					if(winners[i] == winners[z])
    				    {
    						dup = true;
    						if(dup == true)
    					    {
    					    	continue outer;
    					    }
    				    }
    					
    				}
    			}
    			
    			c++;
    		}
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    creates a permanent loop
    The loop control variables must change for the loop to exit. Make sure all the paths the execution takes will change the variables that control the looping, otherwise the loop will go forever.
    Code:
                    dup = true;
    		if(dup == true)
    The if test is useless, it will ALWAYS be true.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0
    It was because the loops were checking the same spot in the array twice, needed to add an & clause to not let the element holding variable be the same either.

    Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo