• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

loopy for loops

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9104
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9104
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:



[ July 21, 2005: Message edited by: Marilyn de Queiroz ]
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic