Results 1 to 20 of 20
  1. #1
    ff123 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default java program help..

    Hi guys.. im new here.. well I need some help.. i was able to create a program which excute the following output

    X
    XX
    XXX
    XXXX
    XXXXX

    but please tell me how can I make this output

    X
    XX
    XXX
    XXXXX
    XXXXXX

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    What you have is

    Java Code:
    X
    XX
    XXX
    XXXX
    XXXXX
    What you want is
    Java Code:
          X
         XX
        XXX
      XXXXX
     XXXXXX
    (You supress html style space mangling by using the code button.)

    Is there some reason why the second example misses the line with four X's? And, if so, what is the rule for determining how many X's go in each line?

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    given your great specs, this is what you're trying to do.

    Java Code:
    System.out.print("     X\n    XX\n   XXX\n XXXXX\nXXXXXX");

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    May be he's looking to do it in a loop I guess. In that case what OP must do is show your effort on it.

  5. #5
    ff123 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    Im using While loop for this program and it uses USER input to give the number of lines.. and i have this out put
    x
    xx
    xxx
    xxxx
    xxxxx

    and im trying to get this like every x centered
    x
    xx
    xxx
    xxxxx
    xxxxxxxx
    Last edited by ff123; 04-11-2009 at 09:14 AM.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    ff123 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    this is my program which gives X on right i want all x to align in center

    int num = 1;
    int num2 = 1;
    int num3 = 1;
    int amount;

    Scanner scan = new Scanner(System.in);

    System.out.print("Enter The Number:");
    amount = scan.nextInt();

    while (num2 <= amount)
    {
    while (num <= num3 )
    {

    System.out.print("x");
    num++;

    }

    System.out.println();
    num3++;
    num = 1;
    num2++;
    }
    }
    }

  8. #8
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    i think num3 should be largest in the beginning... and decrease by 1 in each outer looping

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    if you want to align on the center, then each row will have two more "x" at the edges:
    Java Code:
       x
      xxx
     xxxxx
    xxxxxxx
    There are three things that have to be determined here:
    • 1) the number of rows the triangle will have
    • 2) the number of "x" for each row
    • 3) the number of blank spaces before each row of "x"'s

    1) This is what the user inputs to the program
    2) The number of "x"s per row will always be odd and each row will have two more "x"s than the previous one.
    3) The number of spaces before the rows is one less than the previous row and starts out at: numbers spaces = num rows -1

    What does the above mean? that everything can be done and calculated in a in a loop. If the number of rows is 5, then:
    number of spaces first row = 5 -1 spaces plus one "x"
    second row = 3 spaces plus 3 "x"
    third row = 2 spaces plus 5 "x"
    fourth row = 1 space plus 7 "x"
    fifth row = 0 space plus 9 "x"
    Luck,
    CJSL
    Last edited by CJSLMAN; 04-11-2009 at 03:41 PM. Reason: typos...
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    Shiv is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Smile Here is a soln...!

    import java.io.*;
    public class Hello
    {
    public static void main(String args[])throws IOException
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int n;
    System.out.println("Enter number of lines : ");
    n=Integer.valueOf((String)br.readLine());
    for(int i=0;i<=n;i++)
    {
    for(int j=0;j<n-i+1;j++)
    {
    System.out.print(" ");
    }
    for(int k=0;k<(2*i)-1;k++)
    {
    System.out.print("X");
    }
    System.out.println();
    }
    }
    }


    just copy and paste this code ...!
    and it is working fine..!
    i just hope thats what you require ?
    Thanks

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Not good to do that...

    Shiv... although I know you meant well, you didn't do the OP a favor by giving him a full solution. Doing so doesn't make the OP learn anything. It's too much temption to do a cut, paste and forget (just like you suggested). It is much better to give hints, links, suggestions, snippets, etc. This way the OP learns how to program.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  12. #12
    Shiv is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default Sorry

    Sorry..! I was not aware of that !
    But
    thanks

  13. #13
    ff123 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    thx u have done.. but cant i still go with While loop instead of For loop and if we put 3 as number of lines why your program produces

    Java Code:
    [CENTER]   
      X
     XXX
    XXXX[/CENTER]
    why not
    Java Code:
    [CENTER] 
       X
      XX
     XXX[/CENTER]
    they are all aligned Centered
    Last edited by ff123; 04-12-2009 at 04:46 AM.

  14. #14
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    ff123... because it's centered. you can't center one x over two x's. If you noticed one of my comments in my past posts, I said that the number of x's per row had to be odd (1,3,5,7,etc):
    Java Code:
       x
      xxx
     xxxxx
    xxxxxxx
    Now the interesting part is.... do you know what Shiv's program is doing? It has three "for" loops... do you know what each loop is doing?

    but cant i still go with While loop instead of For loop
    Does this mean that you have to implement this program with while loops?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  15. #15
    ff123 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    hey CJ.. i know what Shiv program loops are doing.. i have done the same program in For Next loop before but not centered.. but CJ can you tell me if its centered why it only produces in Odd numbers?

  16. #16
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    the odd numbers are because you can't center an x over two x's or center two x's over three x's:
    Java Code:
     x
     xx
    xxx
    Unless you want something like this:
    Java Code:
      x
     x x
    x x x
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  17. #17
    ff123 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    the odd numbers are because you can't center an x over two x's or center two x's over three x's:
    Java Code:
     x
     xx
    xxx
    Unless you want something like this:
    Java Code:
      x
     x x
    x x x
    CJSL
    thats intresting CJ.. so can you tell me how can i go with the second output

  18. #18
    ff123 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by ff123 View Post
    thats intresting CJ.. so can you tell me how can i go with the second output
    I have done the second output
    Java Code:
    			for(int num3=0;num3<(1*num);num3++)
    			{
    			    System.out.print(" ");
    				System.out.print("X");
    			}

  19. #19
    ff123 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    CJ can you tell me what each for loop is doing...

  20. #20
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    so can you tell me how can i go with the second output
    huh? I thought from the the following you understood how Shiv's program worked:
    i know what Shiv program loops are doing..
    It shouldn't be too hard to figure out how to change the program to get the second option working (hint: second loop controls the first x of the row, third loop controls the rest of the x's).

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Run program from Java
    By nenadm in forum Advanced Java
    Replies: 4
    Last Post: 12-07-2008, 10:36 PM
  2. Java Program
    By icedragon770 in forum Java Applets
    Replies: 26
    Last Post: 10-12-2008, 12:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  5. help with java program
    By mattvgt in forum New To Java
    Replies: 3
    Last Post: 07-14-2007, 04:57 PM

Posting Permissions

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