Results 1 to 9 of 9
  1. #1
    MooseManX is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Problem getting simple program to work, for loop :(sweat):

    [highlight=Java]
    public class ArgsLoop {
    public static void main(String[] args) {
    int n = Integer.parseInt(args[0]); // now n=5
    for(int i=0;i<n;i++){
    System.out.println();
    System.out.print("*");
    for(int c=i-1; c>-1; c--){
    System.out.print("*");
    }
    }
    }
    }
    [/highlight]

    I'm am trying to get this to output

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

    it does this at the moment but there is a space
    < here
    *
    **
    ***
    ****
    *****

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Problem getting simple program to work, for loop :(sweat):

    Imagine a method exists: printStars(int n) that prints n stars on a line followed by a new line; the following code snippet does the rest:

    Java Code:
    for (int i= 1; i <= 5; i++)
       printStars(i);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default Re: Problem getting simple program to work, for loop :(sweat):

    it does this at the moment but there is a space
    < here
    Not a space but a...

    Java Code:
    for(int i=0;i<n;i++){
      System.out.println(); // ... newline, which you put there
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    MooseManX is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: Problem getting simple program to work, for loop :(sweat):

    It needs that new line though or else it just prints out into one continuous line

  5. #5
    MooseManX is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: Problem getting simple program to work, for loop :(sweat):

    And it needs to find out the number of stars via an argument

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default Re: Problem getting simple program to work, for loop :(sweat):

    Quote Originally Posted by MooseManX View Post
    It needs that new line though or else it just prints out into one continuous line
    Yes, BEFORE or AFTER a line of stars?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    MooseManX is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: Problem getting simple program to work, for loop :(sweat):

    It has to go after, how do I go about doing that? My inner loop relys on that outerloop

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: Problem getting simple program to work, for loop :(sweat):

    Crossposted: How do I do this, code supplied
    @ MooseManX: Stop crossposting, if you ever want to get help on these fora!
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default Re: Problem getting simple program to work, for loop :(sweat):

    Quote Originally Posted by MooseManX View Post
    It has to go after, how do I go about doing that? My inner loop relys on that outerloop
    That is so incredibly trivial, I am not even going to answer that. Think about it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 3
    Last Post: 10-26-2011, 06:20 PM
  2. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 06:12 AM
  3. Problem with very simple program
    By iratus in forum New To Java
    Replies: 13
    Last Post: 11-07-2010, 05:43 PM
  4. Simple Programming problem loop in array
    By katon in forum New To Java
    Replies: 1
    Last Post: 12-20-2009, 04:14 PM
  5. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 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
  •