Results 1 to 6 of 6
  1. #1
    LinxuS is offline Member
    Join Date
    Jan 2008
    Posts
    6
    Rep Power
    0

    Default A little lost with for loops and making a design

    Ask the user to enter a number between 4 and 20. If the user enters a number outside that range ask them for the number again. Based on the number the user enters, print a triangle of stars as in the following example:
    If the user enters 5, your program would print the following:

    *
    **
    ***
    ****
    *****

    (except the alignment is to the right)
    so basically..
    ____*
    ___**
    __***
    _****
    *****
    without the _
    So far, I've got the error checking alright, but I don't really know where to start with for the stars.

    public static void main(String [] args)
    {
    Scanner k = new Scanner(System.in);
    System.out.println("Please enter a number between 4 and 20");
    int num = k.nextInt();

    while(num < 4 || num > 20)
    {
    System.out.println("Oops! Your number is not between 4 and 20, enter another one");
    num = k.nextInt();
    }
    }
    I'm assuming it involves a for loop, with a System.out.print(" "); System.out.print("*"); and a System.out.println("");

    but any more than that I'm not sure..

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
            int n = 5;
            for(int j = 0; j < n; j++)
                System.out.print("*");
            System.out.println();
    You just have to play around with print statements and loops. Imagination and play are the keys to this kind of discovery/exploration.

  3. #3
    dejavujr is offline Member
    Join Date
    Jan 2008
    Posts
    2
    Rep Power
    0

    Default

    int n = 5;
    for(int j = 0; j < n; j++)
    {
    for (int x = 0; x<=j ; x++) {
    System.out.print("*");}

    System.out.println();}

    will dispaly
    *
    **
    ***
    ****
    *****

  4. #4
    LinxuS is offline Member
    Join Date
    Jan 2008
    Posts
    6
    Rep Power
    0

    Default

    well I got this so far..:

    for(int l = 0; l < num; l++)
    {
    for (int a = 0; a <= l; a++)
    System.out.print(" ");

    for (int x = 0; x<=l ; x++)
    {
    System.out.print("*");
    }
    System.out.println();

    }

    but the spaces are in reverse order that they need to be..

  5. #5
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Welcome to the Java Forums. I appreciate you making a valid attempt at your work.

    When working out code like this, there is no better way to get this structure into your head(when you are first starting out), than to get a pencil and a piece of paper and draw out what exactly is going on. Also, choose your variable names wisely.. I changed it to lowercase i because I couldn't figure out if you were using uppercase I, lowercase L or a number one. Cheers. :)

    Please use code tags.
    Java Code:
        int num = 5; 
        int spaces = 5;
        for(int i = 0; i < num; i++) {
          for (int a = spaces - 1; a > 0; a--)
            System.out.print(" ");
    
          for (int x = 0; x <= i; x++) {
            System.out.print("*");
          }
          System.out.println();
          spaces--;
        }
    See you around!
    Last edited by CaptainMorgan; 01-21-2008 at 10:33 PM.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default code tags

    To use code tags you:
    You select your code, and click the hash (#) icon when in advanced mode. :p
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. need help with program im lost
    By lifeturn in forum JCreator
    Replies: 1
    Last Post: 10-28-2008, 07:09 PM
  2. Lost my javadocs
    By orchid in forum Eclipse
    Replies: 3
    Last Post: 04-30-2008, 09:45 PM
  3. Loops (while do etc)
    By manupr in forum New To Java
    Replies: 1
    Last Post: 01-15-2008, 03:59 AM
  4. Absolutely Lost
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 06:25 PM
  5. Help Needed - I'm so lost
    By adlb1300 in forum New To Java
    Replies: 3
    Last Post: 11-14-2007, 01:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •