Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How can I draw a Diamond

  1. #1
    Junior Member
    Join Date
    Dec 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I draw a Diamond

    import java.util.Scanner;

    public class Main
    {
    public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    int x = 0; // input height

    int choice;

    System.out.println("1. Trapezoid ");
    System.out.println("2. Diamaond ");
    System.out.println("3. Hollow Triangle ");
    System.out.println("What kind of shape do you want to see? ");
    choice = input.nextInt(); // user chooses shape

    if(choice==1){
    System.out.print("Enter height: ");
    x = input.nextInt(); //user inputs height

    drawTrapezoid(x);
    } else if(choice == 2){
    System.out.print("Enter height: ");
    x = input.nextInt(); //user inputs height
    drawDiamond(x);
    } else if(choice == 3){
    System.out.print("Enter height: ");
    x = input.nextInt(); //user inputs height
    //drawHollowTriangle(x,mark);
    }



    }

    public static void drawbDiamond(int length){
    for(int j = 1; j<=length; j++){
    System.out.print('x');

    }
    }
    public static void drawbDiamond(int length, char mark){
    for(int j = 1; j<=length; j++){
    System.out.print(mark);

    }
    }
    public static void drawbDiamondln(int length){
    drawbDiamond(length);
    System.out.println();
    }
    public static void drawbDiamondln(int length, char mark){
    drawbDiamond(length,mark);
    System.out.println();
    }
    public static void drawDiamond(int x){
    int dotspace = x - 1;
    int xstars = 1;
    do{ //first half of diamond
    drawbDiamond(dotspace,'.');
    drawbDiamondln(xstars);
    dotspace--;
    xstars+=2;
    } while(dotspace>=0);


    do{
    drawbDiamond(dotspace, '.');
    drawbDiamondln(xstars);
    dotspace++;
    xstars-=2;
    } while(dotspace<=xstars);

    }



    // TRAPEZOID
    public static void drawbTrapezoid(int length){
    for(int i = 1; i<=length; i++){
    System.out.print('x');
    }

    }
    public static void drawbTrapezoid(int length, char mark){
    for(int i = 1; i<=length; i++){
    System.out.print(mark);
    }
    }
    public static void drawbTrapezoidln(int length){
    drawbTrapezoid(length);
    System.out.println();
    }
    public static void drawbTrapezoidln(int length, char mark){
    for(int i = 1; i<=length; i++){
    drawbTrapezoid(length,mark);
    }
    }
    public static void drawTrapezoid(int x){

    int dotspace = x - 1;
    int xstars = 2 * x - 1;
    do{
    drawbTrapezoid(dotspace, '.');
    drawbTrapezoidln(xstars);
    dotspace--;
    xstars+=2;
    } while(dotspace>=0);
    }

    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How can I draw a Diamond

    Do you have any specific java programming questions?
    What does the posted code do when it is compiled and executed?
    Please copy the output and paste it here. Add some comments describing what you want to be different.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    http://www.java-forums.org/misc.php?do=bbcode#code
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: October 20th, 2013, 11:54 PM
  2. Help With Diamond Square Terrain
    By hobbles in forum Algorithms & Recursion
    Replies: 5
    Last Post: May 6th, 2013, 03:10 PM
  3. Diamond perimeter with loop
    By Lanto in forum Loops & Control Statements
    Replies: 5
    Last Post: August 9th, 2012, 12:02 PM
  4. Method to print a diamond
    By snowMac in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 8th, 2011, 04:18 AM
  5. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM

Tags for this Thread