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: While loop help!?

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

    Default While loop help!?

    I have to make a number "triangle that goes like this, using the number 5 as an example:
    ____1
    ___21
    __321
    _4321
    54321

    except the underscores are spaces.

    Here is what I have:

    import java.util.Scanner;
    public class loop1
    {
    public static void main(String[] args)
    {
    Scanner stdIn = new Scanner(System.in);

    System.out.print("Enter a number (1-9): ");
    int n = stdIn.nextInt();
    while (n < 1 || n > 9)
    {
    System.out.print("Error. Enter a number (1-9): ");
    n = stdIn.nextInt();
    }
    System.out.println("You entered " + n + ". Have a nice day.");

    int j=1, k=1;
    while (j<=n)
    {
    k = 1;
    while (k<=n-j)
    {
    System.out.print(" ");
    k++;
    }
    k = 1;
    while (k<=j)
    {
    System.out.print(k);
    k++;
    }
    System.out.print("\n");
    j++;
    }

    }
    }

    and that produces:
    Enter a number (1-9): 5
    You entered 5. Have a nice day.
    ____1
    ___12
    __123
    _1234
    12345

    except the underscores are spaces.

    So my question is, what do I change to have the ones go in the last column?? I've tried changing so many things!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: While loop help!?

    On each line after the spaces you are printing 1 - n. What you need to do is flip that around the print n - 1.
    Improving the world one idiot at a time!

Similar Threads

  1. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  2. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  3. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  4. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  5. loop or what
    By silverspoon34 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 19th, 2009, 02:10 PM