Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Do while loop to generate 6 random numbers

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Do while loop to generate 6 random numbers

    Good evening,
    I am fairly new to Java and I am currently taking a class in the field. My assignment is to generate 6 unique random numbers using a "Do While" expression. I might be mistaken but doesnt the inner loop execute first and then it works its way out? With this logic I believe my code should work but then again its not. Please help me if you can. Here is my code:

    public class DoLottery
    {
    public static void main (String args[])
    {
    int max= 10;
    int random;
    int random2;
    int random3;
    int random4;
    int random5;
    int random6;

    do{
    random= (int)((Math.random()*max)+1);

    do{
    random2= (int)((Math.random()*max)+1);

    do{
    random3= (int)((Math.random()*max)+1);

    do{
    random4= (int)((Math.random()*max)+1);

    do{
    random5= (int)((Math.random()*max)+1);
    random6= (int)((Math.random()*max)+1);

    }while(random6 == random5);

    System.out.println(random5);
    System.out.println(random6);

    }while(random4 == random5 && random4 == random6);

    System.out.println(random4);

    }while(random3 == random4 && random3 == random5 && random3 == random6);

    System.out.println(random3);

    }while(random2 == random3 && random2 == random4 && random2 == random5 && random2 == random6);

    System.out.println(random2);


    }while(random == random2 && random == random3 && random == random4 && random == random5 && random == random6);



    System.out.println(random);

    //System.out.println(random);
    //System.out.println(random2);
    //System.out.println(random3);
    //System.out.println(random4);
    //System.out.println(random5);
    //System.out.println(random6);

    Again, I am a newbie so be gentle....sidenote: I originally had output at the end but decided to comment it out to see if code would execute if I placed it every time the test was true.

    Thanks


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Do while loop to generate 6 random numbers

    Welcome! Please read Announcements - What's Wrong With My Code? to learn how to post code correctly and other useful info for new members.

    use code tag please

    I suggest you to read this link: Java Loops - for, while and do...while
    and analyze how loop works.
    do while loop is a kind of loop that executes atleast once. the last statement in this loop is while(condition) that checks
    if its going to do the same process again (which is the statements inside the do while loop)
    I'll give an example on how to do it
    code below is an example of printing hello world 5 times using do while:
    public class HelloWorld {
     
          public static void main(String[] args) {
                  int counter = 1; // this will the counter, we're going to count for 5
                  do {
                        System.out.println("Hello World");
                        counter = counter + 1; // adds one to counter
                  } while(counter <= 5); // it checks if the counter is equal or less than 5
                  // while statement here is a condition, it will do
                  // the printing as long as counter is less than or equal to 5
          }
    }

    i think you have many nested loop in your code.
    but i think a single do while loop will do.

    if you just need to print random numbers, create a do while loop
    and print inside it a random random, but make sure you create
    a counter that counts 1 - 6. and your condition must be checking
    if your counter reaches 6.
    Last edited by dicdic; March 3rd, 2014 at 12:16 AM.

  3. The Following 2 Users Say Thank You to dicdic For This Useful Post:

    Bran.NET (March 3rd, 2014), GregBrannon (March 3rd, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop to generate 6 random numbers

    Thank you, I will use the code tags in the future. I understand that "Do While" statements will always execute the code once before testing and I understand that the "while" statements will test before the code executes. I understand that i can do this all in one "Do While" statement but for some reason my assignment calls for multiple "Do Whiles". I do like the counter idea. That would eliminate all those variables!

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Do while loop to generate 6 random numbers

    And how will you make sure that the program generates 6 unique random numbers?

  6. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop to generate 6 random numbers

    I have no idea...I have mad 4 different variations and still can not do it

  7. #6
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: Do while loop to generate 6 random numbers

    Hello Bran. How about using an array to store the numbers; then using the nextInt() method from Java.util.Random in a for loop; and finally a simple if statement to ensure the numbers are unique. Wouldn't that simplify everything further and Keep It Simple?
    Who holds the KEY to all knowledge?

Similar Threads

  1. How to generate multiple random numbers?
    By namenamename in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 7th, 2013, 07:34 PM
  2. How to generate random numbers in Java?
    By jls24 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 7th, 2013, 01:31 AM
  3. Generate random numbers between 1 and 52 by passing a seed to Random.
    By Shareefuddin in forum Object Oriented Programming
    Replies: 2
    Last Post: April 22nd, 2013, 09:48 AM
  4. Replies: 1
    Last Post: February 15th, 2013, 08:03 PM
  5. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM