sototo
sototo

Reputation: 3

Even number of line to print diamond

I want to make the diamond pattern with asterisks and the lines of it have to be given by the user. I have problem to make the diamond when the number of lines is even. This is all the code:

import java.util.Scanner;
public class DrawDiam {
    public static void main(String[] args) {
        System.out.println("Please give the number of lines");
        Scanner in = new Scanner(System. in );
        int L = in .nextInt();
        if (L < 4) {
            System.exit(0);
        } else {
            if ((L % 2) != 0) {
                int add = 1;
                int numOfSpaces = L / 2;
                for (int i = 1; i <= L; i++) {
                    for (int j = numOfSpaces; j >= 1; j--) {
                        System.out.print(" ");
                    }
                    for (int j = 1; j <= add; j++) {
                        System.out.print("*");
                    }
                    System.out.println();
                    if (i < (L / 2 + 1)) {
                        add = add + 2;
                        numOfSpaces = numOfSpaces - 1;
                    } else {
                        add = add - 2;
                        numOfSpaces = numOfSpaces + 1;
                    }
                }
            } else {
                int add = 1;
                int numOfSpaces = L / 2;
                for (int i = 0; i <= L + 1; i++) {
                    for (int j = numOfSpaces; j >= 1; j--) {
                        System.out.print(" ");
                    }
                    for (int j = 1; j <= add - 2; j++) {
                        System.out.print("*");
                    }
                    System.out.println();
                    if (i < (L / 2 + 1)) {
                        add = add + 2;
                        numOfSpaces = numOfSpaces - 1;
                    } else {
                        add = add - 2;
                        numOfSpaces = numOfSpaces + 1;
                    }
                }
            }
        }
    }
}

So the program asks for a number >=4.The odd number part is running perfectly but the even number part for L = 6 is coming out like this:

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

for example L = 6 should show this:

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

Upvotes: 0

Views: 1222

Answers (2)

Daniel Maclam
Daniel Maclam

Reputation: 121

Here is my solution. You need to test if you have passed the middle of diamond. If you are at i==L/2 your in the middle and don't increment add or numOfSpaces

public static void main(String[] args) {
    System.out.println("Please give the number of lines");
    Scanner in = new Scanner(System.in);
    int L = in.nextInt();
    if (L < 4) {
        System.exit(0);
    } else {
        if ((L % 2) != 0) {
            int add = 1;
            int numOfSpaces = L / 2;
            for (int i = 1; i <= L; i++) {
                for (int j = numOfSpaces; j >= 1; j--) {
                    System.out.print(" ");
                }
                for (int j = 1; j <= add; j++) {
                    System.out.print("*");
                }
                System.out.println();
                if (i < (L / 2 + 1)) {
                    add = add + 2;
                    numOfSpaces = numOfSpaces - 1;
                } else {
                    add = add - 2;
                    numOfSpaces = numOfSpaces + 1;
                }
            }
        } else {
            int add = 1;
            int numOfSpaces = L / 2;
            for (int i = 0; i < L + 1; i++) {
                for (int j = numOfSpaces; j >= 1; j--) {
                    System.out.print(" ");
                }
                for (int j = 1; j <= add - 2; j++) {
                    System.out.print("*");
                }
                System.out.println();
                if (i < (L / 2)) {
                    add = add + 2;
                    numOfSpaces = numOfSpaces - 1;
                }
                // Edit your else block here:
                else if (i > (L / 2)) {
                    add = add - 2;
                    numOfSpaces = numOfSpaces + 1;
                }
                // End of edit!
            }
        }
    }
}

Upvotes: 1

Cukic0d
Cukic0d

Reputation: 5411

Don't forget to close the Scanner at the end, you need to detect when "i" is the middle of "L", and remove one of the two lines around the middle (cause by example 4/2=2, so the middle is 2 and 3) of it.

This code is working:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        System.out.println("Please give the number of lines");
        Scanner in = new Scanner(System. in );
        int L = in .nextInt();
        if (L < 4) {
            System.exit(0);
        } else {
            int midp = (L/2)+1;
            int midm = (L/2)-1;
            if ((L % 2) != 0) {
                int add = 1;
                int numOfSpaces = L / 2;
                for (int i = 1; i <= L; i++) {
                    for (int j = numOfSpaces; j >= 1; j--) {
                        System.out.print(" ");
                    }    
                    for (int j = 1; j <= add; j++) {
                        System.out.print("*");
                    }
                    System.out.println();
                    if (i < (L / 2 + 1)) {
                        add = add + 2;
                        numOfSpaces = numOfSpaces - 1;
                    } else {
                        add = add - 2;
                        numOfSpaces = numOfSpaces + 1;
                    }
                }
            } else {
                int add = 1;
                int numOfSpaces = L / 2;
                for (int i = 0; i <= L + 1; i++) {
                    if(i != midm){
                        for (int j = numOfSpaces; j >= 1; j--) {
                            System.out.print(" ");
                        }
                        for (int j = 1; j <= add - 2; j++) {
                            System.out.print("*");
                        }
                        System.out.println();
                        if(i == midp){

                        } else if (i < (L / 2 + 1)) {
                            add = add + 2;
                            numOfSpaces = numOfSpaces - 1;
                        } else {
                            add = add - 2;
                            numOfSpaces = numOfSpaces + 1;
                        }
                    }
                }
            }
        }
        in.close();
    }
}

Upvotes: 0

Related Questions