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

标签 java tabs formatting output

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

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

关于java - 为什么我的 Pascal 三角形程序没有提供正确的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31091600/

相关文章:

java - 如何根据第一个元素合并多个数组列表?

PHP - 每个选项卡的 session

python - 如何删除从第四个选项卡到行尾的行中的文本?

visual-studio-code - 由于 "Extension ' JSON 语言功能“无法格式化文件”错误,Visual Studio Code 未格式化

html - 如何更改 SharePoint RichHtmlField 中的段落格式

python - 如何格式化最多 N 位小数的数字,其中 N 是随机的

java - 我怎样才能在我的 mainActivity 中实现这个类

java - 写入 ObjectOutputStream 后如何读取套接字响应?

java - 为 Number 子类生成 boolean 检查

javascript - 如何轻松制作 ionic 标签导航