Howdy, Stranger!

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

Categories

newbie - my first java program

RodeoBurnzRodeoBurnz Member Posts: 47
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

  • beancounterbeancounter Member Posts: 88
    : 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);
  • RodeoBurnzRodeoBurnz Member Posts: 47
    : : 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.
  • Feanors CurseFeanors Curse Member Posts: 70
    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]


  • Shawn CarterShawn Carter Member Posts: 0

    __ ( http://forcoder.org ) free video tutorials and ebooks about || Swift C++ Java R C Go Objective-C Visual Basic .NET Ruby Assembly MATLAB Python Perl PL/SQL JavaScript Delphi C# PHP Scratch Visual Basic Kotlin Bash D Ada Logo FoxPro Scala F# ML Lisp Fortran Crystal Lua Scheme SAS Hack Julia Clojure COBOL Erlang Dart Awk Rust Transact-SQL VBScript Prolog LabVIEW Apex ABAP Alice || _____________

Sign In or Register to comment.