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

Thread: Can someone help me reverse the iteration of this loop?

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

    Default Can someone help me reverse the iteration of this loop?

    I'm writing a program to print an ASCII image using nested for loops.
    The output is a rocket ship that looks like this.

    /**\
    //**\\
    ///**\\\
    ////**\\\\
    /////**\\\\\
    +=*=*=*=*=*=*+
    |../\..../\..|
    |./\/\../\/\.|
    |/\/\/\/\/\/\|
    |\/\/\/\/\/\/|
    |.\/\/..\/\/.|
    |..\/....\/..|
    +=*=*=*=*=*=*+
    |\/\/\/\/\/\/|
    |.\/\/..\/\/.|
    |..\/....\/..|
    |../\..../\..|
    |./\/\../\/\.|
    |/\/\/\/\/\/\|
    +=*=*=*=*=*=*+
    /**\
    //**\\
    ///**\\\
    ////**\\\\
    /////**\\\\\

    I'm having problems with the mid section of the rocket, specifically the bottom part of the mid section:
    |../\..../\..|
    |./\/\../\/\.|
    |/\/\/\/\/\/\|
    I'm having difficulty writing the for loop to correctly print the dots. You're supposed to incorporate a constant which allows you to adjust the size of the overall figure. The loop doesn't work when I adjust the size, only when the value of HEIGHT is 3.
    The loop, however, for some reason works with the top part of the mid section.

    This is the for loop for the top section.
    for (int dots = 1; dots <= -1 * line + 1 * HEIGHT; dots++) {
    System.out.print(".");
    }
    This is the for loop for the bottom section.
    for (int dots = -1 * line + 1 * HEIGHT; dots <= 1; dots++) {
    System.out.print(".");
    }
    Usually reversing the iteration of the loop just requires flipping the conditions, right? But it didn't work this time for some reason. Could someone explain to me why this doesn't work? I can post the code to my entire program if it helps for compiling.


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

    Default Re: Can someone help me reverse the iteration of this loop?

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

    Code tags will also preserve the formatting of ASCII art.

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

    Default Re: Can someone help me reverse the iteration of this loop?

    What I think you need it this:

    - Provide a constant like defaultAmmoutOfCharactersOnLine;
    - Read next line.
    - Calculate ammountOfCharacters in line (rocket/figure)
    - defaultAmmoutOfCharactersOnLine - ammountOfCharacters (substraction)
    -- You now have the ammount of dots you need to display, dotCharsNeeded
    -- Devide dotCharsNeeded by 2.
    -- Before printing out line, print dotsCharsNeeded ammount of dots
    -- Print out line (of rocket/figure).
    -- Print out other half of dots, dotsCharsNeeded.

    So your first line:

    /**\

    This line has 4 characters (ammountOfCharacters = 4). The max is the imaginary value of 12 (defaultAmmountOfCharacters =12)
    Substract 4 from 12. (dotCharsNeeded = 8). Devide this value by 2 (=4), print out 4 dots before /**\ and 4 dots afterwards.

    ..../**\....

    I'm having trouble with the height thing though. I beleive it's unessecary since you're just gonna print out per line anyway.

Similar Threads

  1. Replies: 9
    Last Post: February 26th, 2013, 11:36 AM
  2. Trouble with while loop and the use of the Iteration class.
    By simpson_121919 in forum Loops & Control Statements
    Replies: 7
    Last Post: February 17th, 2013, 10:45 PM
  3. [SOLVED] Method to reverse String using While Loop
    By Montrell79 in forum Loops & Control Statements
    Replies: 2
    Last Post: February 22nd, 2012, 03:49 PM
  4. hash map iteration
    By uniqe_mohini in forum Member Introductions
    Replies: 2
    Last Post: September 10th, 2011, 10:14 AM
  5. While (return value will terminate an iteration or loop?)
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: April 27th, 2010, 09:05 AM