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 2 of 2

Thread: DoWhile

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default DoWhile

    class Alternate
    {
    public static void main(Strin args [])
    {
    int x=0;

    do
    {
    System.out.println(x); x+=1;
    do {System.out.println(x); x+=2;}
    while(x<=10);
    }

    while(x<=10);
    }
    }

    the output is 0 1 3 5 7 9

    but my probLem is ..
    how can i make the output Like this
    0
    2
    1
    4
    3
    6
    5
    8
    7
    10
    9

    Please help me .. thanks Guys

    Bambee


  2. #2
    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: DoWhile

    Please post your code in code tags.

    The repeating pattern, and therefore the part that can be programmed, is:

    2, 1
    4, 3
    6, 5
    8, 7
    10, 9

    Can you program that? If so, stick a 0 on the front, and you're done.

    I did it with a single do/while loop.

    Hint: Don't change the value of x with every print statement.