为什么我的 Pascal 三角形程序没有提供正确的格式?

why my pascal triangle program not giving proper formatting?

我编写自己的逻辑来打印帕斯卡三角形。有以下代码:

import java.util.Scanner;
public class pascal {

    public static void main(String[] arg) {
        Scanner sc = new Scanner(System.in);
        System.out.print("please enter height of pascal triangle :");
        int Heightchk = 0, Height = sc.nextInt();
        if (Height > 0) {
            MYarray PascalArray = new MYarray();
            PascalArray.setLength(Height * 2 - 1);
            PascalArray.IntilizeA(PascalArray.A);
            if (Height == 1) {
                PascalArray.printArray(PascalArray.A);
            } else {
                while (Heightchk < Height) {
                    PascalArray.printArray(PascalArray.A);
                    Heightchk += 1;
                    if (Heightchk < Height) {
                        PascalArray.reSet(PascalArray.B);
                        PascalArray.setElements(PascalArray.A, PascalArray.B);

                        PascalArray.printArray(PascalArray.B);
                        Heightchk += 1;
                        if (Heightchk < Height) {
                            PascalArray.reSet(PascalArray.A);
                            PascalArray.setElements(PascalArray.B, PascalArray.A);
                        }
                    }
                }
            }
        } else {
            System.out.println("Can't Draw pascal Triangle of this Height");
        }
    }
}

class MYarray {
    String[] A, B;
    void IntilizeA(String[] Array) {
        Array[(Array.length - 1) / 2] = "1";
    }
    void setLength(int length) {
        A = new String[length];
        B = new String[length];
        for (int i = 0; i < A.length; i++) {
            A[i] = "\t";
            B[i] = "\t";
        }
    }
    void reSet(String[] Array) {
        for (int i = 0; i < Array.length; i++) {
            Array[i] = "\t";
        }
    }
    void printArray(String[] Array) {
        for (String Element : Array) {
            System.out.print(Element);
        }
        System.out.println();
        System.out.println();
        System.out.println();
    }
    void setElements(String[] from, String[] to) {
        for (int i = 0; i < from.length; i++) {
            if (from[i] != "\t") {
                if (to[i - 1] == "\t") {
                    to[i - 1] = "0";
                }
                if (to[i + 1] == "\t") {
                    to[i + 1] = "0";
                }
                to[i - 1] = String.valueOf(Integer.valueOf(to[i - 1]) + Integer.valueOf(from[i]));
                to[i + 1] = String.valueOf(Integer.valueOf(to[i + 1]) + Integer.valueOf(from[i]));
            }
        }
    }
}

我应用的逻辑工作得非常好,但是元素对齐仍然存在问题。 它给出以下输出:

    please enter height of pascal triangle :5
                1               


            1   1           


        1   2   1       


    1   3   3   1   

虽然它的输出应该是这样的:

    please enter height of pascal triangle :5
                1               


            1       1           


        1       2       1       


    1       3       3       1   


1       4       6       4       1

我的逻辑有什么问题。我怎样才能做对?

既然知道高度,就可以推断出最后一行的宽度。其中一半是中间,这是第一行应该水平开始的地方。对于每增加一行,在中间之前再开始几个空格。

你的代码绝对没问题。但是,当您用任何数字替换制表符 \t 时,您最终会删除该数组的那么多缩进。

例如,在您的第一个数组中 1 是第 4 个元素,它是中心元素。这意味着它在数组中心之前有 4 个选项卡。

而在第二个数组中,在中心索引之前,您已将其中一个制表符替换为 1,因此第二个数组的缩进和最终的数组中心将向左移动 1 个制表符。

下一个阵列继续如此。所以你的格式出错了。要处理此问题,请不要将制表符替换为数字,而是将数字附加到 \t。这将确保您的缩进完好无损。

以下是更新后的代码。我还更新了您的代码以遵循标准命名约定

import java.util.Scanner;
public class Pascal {

    public static void main(String[] arg) {
        Scanner sc = new Scanner(System.in);
        System.out.print("plese enter height of pascal tringle :");
        int heightchk = 0, height = sc.nextInt();
        if (height > 0) {
            MyArray pascalArray = new MyArray();
            pascalArray.setLength(height * 2 - 1);
            pascalArray.IntilizeA(pascalArray.a);
            if (height == 1) {
                pascalArray.printArray(pascalArray.a);
            } else {
                while (heightchk < height) {
                    pascalArray.printArray(pascalArray.a);
                    heightchk += 1;
                    if (heightchk < height) {
                        pascalArray.reSet(pascalArray.b);
                        pascalArray.setElements(pascalArray.a, pascalArray.b);

                        pascalArray.printArray(pascalArray.b);
                        heightchk += 1;
                        if (heightchk < height) {
                            pascalArray.reSet(pascalArray.a);
                            pascalArray.setElements(pascalArray.b, pascalArray.a);
                        }
                    }
                }
            }
        } else {
            System.out.println("Can't Drow pascal Tringle of this Height");
        }

        sc.close();
    }
}

class MyArray {
    String[] a, b;
    void IntilizeA(String[] array) {
        array[(array.length - 1) / 2] = "\t1";
    }
    void setLength(int length) {
        a = new String[length];
        b = new String[length];
        for (int i = 0; i < a.length; i++) {
            a[i] = "\t";
            b[i] = "\t";
        }
    }
    void reSet(String[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = "\t";
        }
    }
    void printArray(String[] array) {
        for (String element : array) {
            System.out.print(element);
        }
        System.out.println();

    }
    void setElements(String[] from, String[] to) {
        for (int i = 0; i < from.length; i++) {
            if (from[i] != "\t") {
                if (to[i - 1] == "\t") {
                    to[i - 1] = "0";
                }
                if (to[i + 1] == "\t") {
                    to[i + 1] = "0";
                }
                to[i - 1] = "\t"+String.valueOf(Integer.valueOf(to[i - 1].trim()) + Integer.valueOf(from[i].trim()));
                to[i + 1] = "\t"+String.valueOf(Integer.valueOf(to[i + 1].trim()) + Integer.valueOf(from[i].trim()));
            }
        }
    }
}

输出

                1               
            1       1           
        1       2       1       
    1       3       3       1   
1       4       6       4       1