Search code examples
javadrjava

Wrong shape when program run


import java.util.Scanner;
public class Ideone
{

    public static void main(String[] args) 
    {
        int reader;
        Scanner kBoard = new Scanner(System.in);
        do
        {
            System.out.println("Insert a number of rows: ");

                reader = kBoard.nextInt();
                printDiamond(reader); 


        }while(reader != 0);

    }

    public static void printWORD(int n)
    {
        if(n >= 1)
        {
          System.out.print("SAMPLE");
            printWORD(n - 1);
        }
    }

    public static void printTopTriangle(int rows)
    {
        int x = 1;
        for(int j = (rows - 1); j >= 0; j--,x +=2)
        {
            printSpaces(j);
            printWORD(x);
            System.out.print("\n");
        }
    }

    public static void printSpaces(int n)
    {
        if(n >= 1)
        {
            System.out.print(" ");
            printSpaces(n - 1);
        }
    }

    public static void printBottomTriangle(int rows, int startSpaces)
    {
        int x = 1 + (2*(rows - 1));
        for(int j = startSpaces; j <= (rows) && x > 0; j++,x -=2)
        {
            printSpaces(j);
            printWORD(x);
            System.out.print("\n");
        }
    }
    public static void printBottomTriangle(int rows)
    {
        int x = 1 + (2*(rows - 1));
        for(int j = 0; j <= (rows - 1) && x > 0; j++,x -=2)
        {
            printSpaces(j);
            printWORD(x);
            System.out.print("\n");
        }
    }

    public static void printDiamond(int rows)
    {
        printTopTriangle((int)rows/2 + 1);
        printBottomTriangle((int)rows/2, 1);
    }
}

My program is supposed to show a Diamond Shape made out of the word "SAMPLE." But when I run it, it displays a space ship shape. How do I fix this error so it would print a perfect Diamond with the word "SAMPLE"?


Solution

  • Change these methods as follows:

    public static void printTopTriangle(int rows)
       ...
       printSpaces(j*6);
    
    
    
    public static void printBottomTriangle(int rows, int startSpaces)
       ...
       printSpaces(j*6);
    

    Note: 6 is the length of the constant SAMPLE