java - 打印数字菱形无法正常工作

标签 java loops ascii-art

我的菱形应该以模式打印数值。当输入为 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();
    }
}

希望你能明白!!

关于java - 打印数字菱形无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42686542/

相关文章:

linux - 如何移动 figlet 输出

matlab - 使用 Matlab/Octave 绘制字符模式(shell)图?

java - 使用 MyBatis 和 Spring 进行 Oracle 数据库轮询

java - 使用准备好的语句插入单行

Java 8 forEach 用例

java - java中的for循环内部的for循环

带 float 的 Python xrange

java - Google App Engine- java.lang.IllegalStateException : Committed Error

c++结构图中的值确实以隐藏的方式发生变化

python - 用 Python 制作钻石 ASCII 艺术