newbie - my first java program

Could anyone please help? I need a program that outputs a diamond like the other below:
_____*
____***
___*****
____***
_____*
(the underscores are just spaces, the diamond wouldn't come out right when i tried to make it without them)

I dont know if I'm supposed to use a for loop or what. Someone please help me! Thank you.

Comments

  • : Could anyone please help? I need a program that outputs a diamond like the other below:
    : _____*
    : ____***
    : ___*****
    : ____***
    : _____*
    : (the underscores are just spaces, the diamond wouldn't come out right when i tried to make it without them)
    :
    : I dont know if I'm supposed to use a for loop or what. Someone please help me! Thank you.
    :
    Not very clean but it'll get you started

    String star = "*";
    System.out.println(" "+star);
    System.out.println(" "+star +star + star + star +star);
    System.out.println(" "+star +star +star);
    System.out.println(" "+star);
  • : : Could anyone please help? I need a program that outputs a diamond like the other below:
    : : _____*
    : : ____***
    : : ___*****
    : : ____***
    : : _____*
    : : (the underscores are just spaces, the diamond wouldn't come out right when i tried to make it without them)
    : :
    : : I dont know if I'm supposed to use a for loop or what. Someone please help me! Thank you.
    : :
    : Not very clean but it'll get you started
    :
    : String star = "*";
    : System.out.println(" "+star);
    : System.out.println(" "+star +star + star + star +star);
    : System.out.println(" "+star +star +star);
    : System.out.println(" "+star);
    :
    Thank you for your reply. Is there a way to do this using loops?(I think that is what I'm supposed to do). Please help. Thank you.
  • Here, try this programm:

    [code]
    public class Diamond {
    private static final int ROWS = 5; // Has to be odd

    public static void main (String [] args) {
    for (int i = 0; i < ROWS; i++) {

    // print space
    for (int j = 0; j < abs(ROWS/2-i); j++)
    System.out.print (" ");

    // print stars
    for (int j = 0; j < (ROWS/2-abs(ROWS/2-i))*2+1; j++)
    System.out.print ("*");

    // new row
    System.out.println ("");
    }
    }


    // return the absolute (positive) int of the paramter
    private static int abs (int number) {
    if (number > 0)
    return number;

    return -number;
    }
    }
    [/code]

    You may vary the size of the diamond by changing ROWS.

    [size=1][Blue]For our true nature is sin
    [B]Cradle of Filth[/b]


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories