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: Begginer / why is this not printing? + loop not working

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Begginer / why is this not printing? + loop not working

    Hello,

    I am trying to make a program that prints triangle... and I did various test on each method to realise that the problem lies with this segment.

    When I call this method, nothing prints out, I figure there is something with the loop that I am not realizing.

    P.S the loop is backwards because it's supposed to have the right side edge parralel (when I try to print it out the spaces do not appear, imagine the x are space...), so as each line is looped the # of spaces diminishes
    xxxx*
    xxx*x*
    xx*xx*
    x*xxx*
    *****

    If anyone would be so kind as to help me out it would be greatly appreciated

    Christian

    public class test {
     
        public static void main(String[] args){   
     
            for (int countdown = 5; countdown <= 1; countdown = countdown--){     
                showNTimes(countdown, ' ');
                showNTimes(5- countdown, '*'); 
                System.out.println("");
            }
        }
     
        public static void showNTimes ( int nbTimes, char carac ) {
            for ( int i = 1 ; i <= nbTimes ; i = i + 1 ) {
                System.out.print( carac );
            }
        } 
    }


  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: Begginer / why is this not printing? + loop not working

    your loop in your main method has initialization of countdown = 5 and a condition of countdown must be less than or equal 1 therefore that loop will never be executed.

    Once you fix that bug regarding with the condition, take a look on your decrement. You might have a problem with the decrement countdown = countdown--, if you just want to decrease the current value of your countdown by 1, this statement (decrement) will do best: countdown--. The problem will occur since you used post decrement/increment.

    Post decrement/increment will execute the statement first before the increment/decrement.
    Pre Inrement will execute first the increment/decrement before the main statement
    Last edited by dicdic; July 8th, 2014 at 02:33 AM.

Similar Threads

  1. [SOLVED] Begginer ~ why is my divider not working?
    By Christian_Doucet in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2014, 12:54 AM
  2. Printing loop twice?
    By NTWolf1220 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 4th, 2013, 10:18 AM
  3. How tp printing array elements vertically through loop.
    By bikes_28 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 6th, 2012, 01:51 PM
  4. HELP: UNABLE TO USE NON-PRINTING CHARACTERS IN A LOOP
    By baraka.programmer in forum Loops & Control Statements
    Replies: 5
    Last Post: June 24th, 2011, 02:21 AM
  5. Triangle printing (for loop)
    By thirdwave in forum Loops & Control Statements
    Replies: 2
    Last Post: December 6th, 2010, 04:07 PM