JustTryingToCode
JustTryingToCode

Reputation: 119

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...
        }
    }
}

Upvotes: 1

Views: 499

Answers (2)

RaffleBuffle
RaffleBuffle

Reputation: 5455

Another way to tackle this is to determine expressions for the number of spaces and asterisks in each line of the star. Obviously this will need to be in terms of the number of lines (n) and the line we're on.

If we number lines from i = 0 to n-1 we get

nAsterisks = 1 + 2 * min(i, n-i-1)

and

nSpaces = (n+1)/2 - 1 - min(i, n-i-1)

With these we can write some compact Java:

static void printStar(int n)
{
    for(int i=0, k=0; i<n; i++, k=Math.min(i, n-i-1))
    {
        for(int j=0; j<(n+1)/2-1-k; j++) System.out.print(" ");         
        for(int j=0; j<1+2*k; j++) System.out.print("*");           
        System.out.println();
    }
}

For printStar(8) we get:

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

and printStar(9) gives

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

Upvotes: 0

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79015

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();
    }
}

Upvotes: 1

Related Questions