Search code examples
javadrawshapes

Drawing a Diamond with User Input (not double of the user input)


So I've been trying to draw a Diamond shape in Java and I've got the top part of the diamond done but the bottom part is not printing as I want it to. Instead of decreasing towards the end, it stays the same or it increases as it go down.

enter image description here

import java.util.Scanner;

public class Q1_Diamond{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.print("How many lines? ");
        int lines = sc.nextInt();

        // Top half of Diamond
        for(int i = 0; i < lines/2; i++){
            for(int j = 0; j < (lines-1)-i; j++){
                System.out.print(" ");
            }

            for(int j = 0; j < i; j++){
                System.out.print("*");
            }

            for(int j = 0; j < i+1; j++){
                System.out.print("*");
            }
            System.out.println();
        }

        // Bottom half of Diamond
        // Even number of lines
        if(lines % 2 == 0){
            for(int k = 0; k < (lines/2); k++){

                for(int j = 0; j < (lines/2)+k; j++){
                    System.out.print(" ");
                }

                for(int j = 0; j < (lines/3 - 0.5f); j++){
                    System.out.print("*");
                }

                for(int j = 0; j < lines/2+1; j++){
                    System.out.print("*");
                }
                System.out.println();
            }
        }

        // Odd number of lines
        else{
            not done yet...
        }
    }
}

Solution

  • Remove the if condition (i.e. if(lines % 2 == 0)) for the lower half and simply repeat the same code as the upper half with the following loop declaration:

    for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--) 
    

    Complete code:

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("How many lines? ");
            int lines = sc.nextInt();
    
            // Top half of Diamond
            for (int i = 0; i < lines / 2; i++) {
                for (int j = 0; j < (lines - 1) - i; j++) {
                    System.out.print(" ");
                }
    
                for (int j = 0; j < i; j++) {
                    System.out.print("*");
                }
    
                for (int j = 0; j < i + 1; j++) {
                    System.out.print("*");
                }
                System.out.println();
            }
            // Bottom half of Diamond
            for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--)  {
                for (int j = 0; j < (lines - 1) - i; j++) {
                    System.out.print(" ");
                }
    
                for (int j = 0; j < i; j++) {
                    System.out.print("*");
                }
    
                for (int j = 0; j < i + 1; j++) {
                    System.out.print("*");
                }
                System.out.println();
            }
        }
    }
    

    A sample run:

    How many lines? 10
             *
            ***
           *****
          *******
         *********
         *********
          *******
           *****
            ***
             *
    

    Another sample run:

    How many lines? 11
              *
             ***
            *****
           *******
          *********
         ***********
          *********
           *******
            *****
             ***
              *
    

    [Update]

    You can extract the common code into a method e.g. printLine as shown below:

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("How many lines? ");
            int lines = sc.nextInt();
    
            // Top half of Diamond
            for (int i = 0; i < lines / 2; i++) {
                printLine(lines, i);
            }
            // Bottom half of Diamond
            for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--) {
                printLine(lines, i);
            }
        }
    
        static void printLine(int lines, int i) {
            for (int j = 0; j < (lines - 1) - i; j++) {
                System.out.print(" ");
            }
    
            for (int j = 0; j < i; j++) {
                System.out.print("*");
            }
    
            for (int j = 0; j < i + 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }