File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes loopy for loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "loopy for loops" Watch "loopy for loops" New topic
Author

loopy for loops

Ali Gilani
Ranch Hand

Joined: Mar 01, 2002
Posts: 137
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

Joined: Jul 22, 2000
Posts: 9044
    
  10
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 ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Jul 22, 2000
Posts: 9044
    
  10
Try this:



[ July 21, 2005: Message edited by: Marilyn de Queiroz ]
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
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.)


SCJP 1.4 (90%)<br />SCWCD 1.4 (88%)
Daniel .J.Hyslop
Ranch Hand

Joined: May 23, 2005
Posts: 55
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.


an island in the sun <br />with a language of many tongue?
Ali Gilani
Ranch Hand

Joined: Mar 01, 2002
Posts: 137
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: loopy for loops
 
Similar Threads
Integer.toString.......
Program keeps locking up, not sure why
Coding
Passing parameters to functions help.
Bad sentinel loop structure for intro to java class