• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loopy for loops

 
Ali Gilani
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
heres the excercise i have to do. a lottery program that randomly selects 6 numbers from 1 to 49 (with no repeats), and gives 5 sets of numbers.




Problem: 'if(number > 50 | (number == pnum)' although numbers above 50 are not used, often two numbers repeat in a single set (after running the program for about 6 times)

problem2: the output it something like:

34
32
16
10
8
19
--------------- (execution stops here)

how do i get the rest 4 sets, and the numbers to be printed on a single line?


Ali
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem: 'if(number > 50 | (number == pnum)' although numbers above 50 are not used, often two numbers repeat in a single set (after running the program for about 6 times)

Rather than using 'continue', I suggest using if/else where if the condition is true, you do nothing.


problem2:

how do i get the rest 4 sets, and the numbers to be printed on a single line?


I thought you only wanted 6 results, now you want the other 4?
single line, use System.out.print rather than println
[ July 21, 2005: Message edited by: Marilyn de Queiroz ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's one way

load the numbers 1 to 49 into an array, or arraylist

Collections.shuffle(..)
shuffle 'em a few times, if you want
then take the first 6 numbers (0 to 5)

repeat as many times as you want
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:



[ July 21, 2005: Message edited by: Marilyn de Queiroz ]
 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have put a break once your count reaches 0. So only one set of numbers is printed. You can have another counter whic can be initialised to 5 and decfremented every time you print a "--------------".
Then break the for loop only when this new counter becomes zero.(Put another if condition for that.)
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ali,
If you put a println statment below your number generator I think it may help you to see the reason why you are getting duplicate numbers , just ponder on what the second test in the first if statement is trying to achieve.
 
Ali Gilani
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem resolved, Swati thanks for the help.

Daniel: riddles are not for forums. you should be more helpful. the duplication occurs as all 5 numbers must be compared not just the previous one, as told in the sun forums

Ali
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic