打印数字菱形不能正常工作

Printing numeric rhombus doesn't work properly

我的菱形应该以某种模式打印数值。当输入为 7 时它可以工作,但是当我尝试将其重新编程为循环时,对于任何给定的数字,它只输出 1。请帮忙。任何批评都会有所帮助。这是我的代码,适用于 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--;
        }
    }
}

这是我的代码,当它只为任何给定值打印一个时。

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

希望你能明白!!

-nn 的两个嵌套 for 循环 和一个 if else 语句。零点在菱形的中心。

Try it online!

int n = 6;
for (int i = -n; i <= n; i++) {
    for (int j = -n; j <= n; j++)
        if (Math.abs(i) + Math.abs(j) <= n)
            System.out.print(Math.abs(j)+1);
        else
            System.out.print(" ");
    System.out.println();
}

输出:

      1      
     212     
    32123    
   4321234   
  543212345  
 65432123456 
7654321234567
 65432123456 
  543212345  
   4321234   
    32123    
     212     
      1      

另请参阅:
How to print a diamond of random numbers?
Drawing numeric diamond