Java

Discussion in 'School Work Help' started by Shirlie, Mar 25, 2010.

  1. Shirlie

    Shirlie Member

    8
    26
    0
    How do I print this out using for loops?:

    ....*
    ...**
    ..***
    .****
    *****
    .****
    ..***
    ...**
    ....*


    Without the "...." just the "*". It's like in a diamond shape.
     
    #1 Shirlie, Mar 25, 2010
    Last edited: Mar 26, 2010
  2. spider-man

    spider-man Well-Known Member

    466
    55
    1
    I tired, but it's too hard.-tongue2
     
  3. spider-man

    spider-man Well-Known Member

    466
    55
    1
    for(int i = 1; i <= 5; i++)
    {
    for(int j = 1; j < i; j++)
    {
    for(int a = 4; a = a+1 ; a--)
    ///////I give up.
    {
    }
    System.out.print("*");
    }
    System.out.println();
    }
     
  4. hmmm just trying to clarify...

    ....*
    ...**
    ..***
    .****
    .*****
    .****
    ..***
    ...**
    ....*

    and not

    ....*
    ..***
    *****
    ..***
    ....*

    cuz if you're gonna be spacing out the asterisk with a space " " it's gonna be offset.

    only thing i can think of that might work is:

    ....*
    ...* *
    ..* * *
    .* * * *
    * * * * *
    .* * * *
    ..* * *
    ...* *
    ....*


    is that what you were looking for? it depends what results you want, so we can help you with getting the method for the proper result
     
  5. spider-man

    spider-man Well-Known Member

    466
    55
    1

    This:
    ....*
    ...**
    ..***
    .****
    *****
    .****
    ..***
    ...**
    ....*

    Replace the "..." with spaces. This is too hard. I believe you have to do this in two parts.
     
  6. jon, i corrected my post.

    in the courrier font, the width of all characters are equal.

    ....*
    ...* *
    ..* * *
    .* * * *
    * * * * *
    .* * * *
    ..* * *
    ...* *
    ....*


    you notice how there is a space between each asterisks? my concern is not the spacing of "...." my concern is trying to properly space out so that:

    ....*
    ...* *

    there is one * on top of two **, and centered.

    do you see?
     
  7. spider-man

    spider-man Well-Known Member

    466
    55
    1

    The space between the asterisks doesn't matter because when you do a print out, you can print it out like:
    System.out.print("* ");
    or
    System.out.print("*");
    It's easy to change between the two.

    The first post doesn't have space between asterisks.
     
  8. lol thanks jon. i am fully aware of that fact. but i was not talking about spaces after the end of a string.

    I know it doesn't. i was trying to get clarification from OP to make sure there was no mistake in understanding the question.

    my question still remains:

    [​IMG]

    how do you get half a space?

    you can't.

    which brings me to the idea that if there are spaces between the asterisks, you provide each row with an odd number of characters, which will allow you to CENTER one row on top of another.
     
  9. spider-man

    spider-man Well-Known Member

    466
    55
    1

    HAHAHAHA, I didn't think about that. -woot2 Ya, you need a space between the asterisks.
    I think you have to use:
    System.out.print("* ");
     
  10. spider-man

    spider-man Well-Known Member

    466
    55
    1
    looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooool
    o...The way the asterisks are displayed here, made me think they are centered...o
    looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooool

    (lol around the sentence)
     
    #10 spider-man, Mar 26, 2010
    Last edited: Mar 26, 2010
  11. iiimj4everiii

    iiimj4everiii Well-Known Member

    211
    241
    0
    if you can manage to use the absolute value then it'll work. i havent c++ for a while. but if you can manage to set the j = 5 - abs(x). abs(x) is the # of dots and j is the number of stars. work something with it. it should work
     
  12. fong06

    fong06 Member

    16
    26
    0
    Made this assignment 4 years ago too lol...

    /*
    At the execution, give a integer as argument. If you using linux at terminal then =>
    for example : java Diamant 8

    Something like this will work
    you need to include the math class too
    */

    Code:
    public class Diamant{
    
        public static void main(String[] args) { 
            int N = Integer.parseInt(args[0]);  
    
            for (int i = -N; i <= N; i++) {
                for (int j = -N; j <= N; j++) {
                    if (Math.abs(i) + Math.abs(j) <= N) System.out.print("* ");
                    else                                System.out.print("  ");    
                }
                System.out.println();
            }
        }
    }
    
    /* output

    Code:
            *         
          * * *       
        * * * * *     
      * * * * * * *   
    * * * * * * * * * 
      * * * * * * *   
        * * * * *     
          * * *       
            *         
    if you want this then delete the extra space sign @
    System.out.print("* "); and (" ) =>

    Code:
        *    
       ***   
      *****  
     ******* 
    *********
     ******* 
      *****  
       ***   
        *   
    */
     
    #12 fong06, Mar 27, 2010
    Last edited: Mar 27, 2010
  13. lol, that's with rows containing odd numbers:

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

    the same can be applied to increments of one

    *
    **
    ***
    **
    *
     
  14. Shirlie

    Shirlie Member

    8
    26
    0
    Sorry, I didn't know when I post this, the spacing between the stars disappear in a regular post.


    Code:
        *
       * *
      * * *
     * * * *
    * * * * *
     * * * *
      * * *
       * *
        *