Skip to content
Advertisement

Printing numeric rhombus doesn’t work properly

My rhombus is supposed to print numeric values in a pattern. It works when the input is seven, but when I tried reprogramming it to go in a loop, that is for any given number, it just outputs 1. Please help. Any criticism would be helpful. Here is my code for when it works for 7.

import java.util.Scanner;

public class NumericRhombus1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter amount: ");
        int total = sc.nextInt();
        int SpacesAmount = total;
        int n = 1;
        int x = 0;
        int num = 1;
        int w = 0;
        for (int i = 1; i <= total; i++) {
            for (int j = SpacesAmount; j != 0; j--) {
                System.out.print(" ");
            }
            for (int a = 1; a <= 2 * i - 1; a++) {
                if (a == 1 || a == (2 * i - 1))
                    System.out.print(n + "");
                else if (a == 2 || a == (2 * i - 1) - 1)
                    System.out.print(n - 1 + "");
                else if (a == 3 || a == (2 * i - 1) - 2)
                    System.out.print(n - 2 + "");
                else if (a == 4 || a == (2 * i - 1) - 3)
                    System.out.print(n - 3 + "");
                else if (a == 5 || a == (2 * i - 1) - 4)
                    System.out.print(n - 4 + "");
                else if (a == 6 || a == (2 * i - 1) - 5)
                    System.out.print(n - 5 + "");
                else if (a == 7 || a == (2 * i - 1) - 6)
                    System.out.print(n - 6 + "");
                else {
                    System.out.print("0");
                }
            }
            System.out.println();
            n++;
            w++;
            num++;
            SpacesAmount--;
        }
        System.out.print(" ");
        int y = n - 1;
        for (int i = total - 1; i >= 1; i--) {
            if (i == 1 || i == 2)
                System.out.print("");
            for (int j = 1; j <= total - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                if (j == 1 || j == (2 * i - 1))
                    System.out.print((y - 1) + "");
                else if (j == 2 || j == (2 * i - 1) - 1)
                    System.out.print((y - 2) + "");
                else if (j == 3 || j == (2 * i - 1) - 2)
                    System.out.print((y - 3) + "");
                else if (j == 4 || j == (2 * i - 1) - 3)
                    System.out.print((y - 4) + "");
                else if (j == 5 || j == (2 * i - 1) - 4)
                    System.out.print((y - 5) + "");
                else if (j == 6 || j == (2 * i - 1) - 5)
                    System.out.print((y - 6) + "");
                else if (j == 7 || j == (2 * i - 1) - 6)
                    System.out.print(y - 7 + "");
                else
                    System.out.print("0");
            }
            System.out.println();
            System.out.print(" ");
            y--;
        }
    }
}

Here is my code for when it just prints one for any given value.

import java.util.Scanner;

public class NumericRhombus2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter amount: ");
        int total = sc.nextInt();
        int SpacesAmount = total;
        int n = 1;
        int x = 0;

        int num = 5;
        int r = 0;
        int ser = 0;
        int re = 1;
        int until = total - 2;
        for (int i = 1; i <= total; i++) {
            for (int j = SpacesAmount; j != 0; j--) {
                System.out.print(" ");
            }
            for (int a = 1; a <= 2 * i - 1; a++) {
                if (a == num || a == (2 * i - 1) - r)
                    System.out.print(n - ser + "");
                else {
                    System.out.print("1");
                }
            }
            n++;
            num++;
            r++;
            ser++;
            SpacesAmount--;
            System.out.println();

        }
        System.out.print(" ");
        int y = n - 1;
        for (int i = total - 1; i >= 1; i--) {
            if (i == 1 || i == 2)
                System.out.print("");
            for (int j = 1; j <= total - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                if (j == num || j == (2 * i - 1) - r)
                    System.out.print((y - re) + "");
                else
                    System.out.print("1");
            }
            System.out.println();
            System.out.print(" ");
            num++;
            r++;
            y--;
        }
    }
}

Advertisement

Answer

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter amount: ");
    int num = sc.nextInt();
    int r, SpacesAmount, re;
    for (r = 1; r <= num; r++) {
        for (SpacesAmount = num - r; SpacesAmount > 0; SpacesAmount--)
            System.out.print(" ");
        for (re = r; re >= 1; re--)
            System.out.print(re);
        for (re = 2; re <= r; re++)
            System.out.print(re);
        System.out.println();
    }
    for (r = 1; r <= num; r++) {
        for (SpacesAmount = r; SpacesAmount >= 1; SpacesAmount--)
            System.out.print(" ");
        for (re = num - r; re >= 1; re--)
            System.out.print(re);
        for (re = 2; re <= num - r; re++)
            System.out.print(re);
        System.out.println();
    }
}

Hope you get it!!

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement