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: Help with the incrementing in a For loop

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

    Default Help with the incrementing in a For loop

    I have an assignment that follows " Write a code segement using a for loop that, given a positive integer n, prints first n postive integers, such that each integer is preceded by n number of asterisks. EX. if n = 3 output would be *1**2***3

    I have the loop working correctly enough that it prints an asterisk and the incrementing numbers, but I can't seem to figure out to get the correct number of asterisks before each number. Here is what I have so far.

    for (int x = 1; x <= n; x++)
    {
    System.out.println("*");
    System.out.println(x);
    }

    what that would print is like n = 3 output would be *1*2*3. How do I get the asterisks to increment as well? THANKS!


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with the incrementing in a For loop

    I finally figured it out. Had to nest two for loops!

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Help with the incrementing in a For loop

    Hi and welcome.

    ...click...click...click...Ding!

    Glad you got it. Please mark the thread as solved. Instructions here.

Similar Threads

  1. [SOLVED] What wrong with my code? count++ isnt incrementing.
    By godofdoom999 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 24th, 2012, 12:37 PM
  2. Problems with incrementing/decrementing in a for loop..
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 13th, 2011, 05:15 AM
  3. Incrementing button by 1
    By joshft91 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 1st, 2011, 09:45 AM
  4. Return from incrementing for loop?
    By mindlessn00b in forum Loops & Control Statements
    Replies: 16
    Last Post: October 4th, 2010, 12:15 PM
  5. Return from incrementing for loop HELP?!
    By mindlessn00b in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 3rd, 2010, 02:36 PM