Java 帕斯卡';s的三角形图案突然断裂

Java 帕斯卡';s的三角形图案突然断裂,java,pascals-triangle,Java,Pascals Triangle,下面的程序打印Pascal三角形。 该程序对于较小的输入工作正常,但如果您向构造函数传递的值大于13,则该模式将中断。我不明白为什么,需要帮助。还请评论我的编码风格: /** Following program prints the Pascal triangle * * read from bottom to top to understand the implementation */ public class PascalTriangle { int noOfRows;

下面的程序打印Pascal三角形。 该程序对于较小的输入工作正常,但如果您向构造函数传递的值大于13,则该模式将中断。我不明白为什么,需要帮助。还请评论我的编码风格:

/** Following program prints the Pascal triangle 
 * 
 * read from bottom to top to understand the implementation
 */
public class PascalTriangle {

    int noOfRows;

    PascalTriangle(int noOfRows)                    /// user have to provide how many lines of Pascal's triangle he wants to print
    {
        this.noOfRows = noOfRows;
    }
    private int factorial(int n)                    /// this method calculate factorial which we use to calculate the NCR
    {  
        int fact = 1;
        for(int i = 2 ; i <= n ; ++i)
            fact*=i;
        return fact;
    }
    private void printSpaces(int spaceCount)        /// this method prints spaces before the first elements of every line
    {
        for(int i = 1 ; i < spaceCount ; ++i)  
            System.out.print("   ");
    }
    private void printElement(int n , int r)                                /// this method prints an element
    {   
        System.out.printf("%3d",factorial(n)/(factorial(n - r) * factorial(r)));  /// calculate NCR for given no of row and given r
        System.out.print("   ");                                             /// print two spaces after every element
    }
    private void printRow(int rowNumber)           /// this method prints elements of a row
    {  
        int r = 0;                                 /// to calculate NCR r = 0 is always zero for first element of a row
        int noOfElements = rowNumber + 1;          /// Each row contains one element more than which row it is 
        for(int i = 1 ; i <= noOfElements ; ++i)   /// run this loop until you print all elements of that row
        {
        printElement(rowNumber , r);               /// call the printElement method and tell it for which row is it going to print the element and what is r for that element
        ++r;                                       /// increase r for every element;
        }
    }
    public void printPascalTriangle()       /// this function prints the Pascal's Triangle
    {
        int rowNumber = 0;                  /// this variable decides which row of the Pascal's Triangle should print 
        int spaceCount = noOfRows;
        while(rowNumber < noOfRows)         /// print rows of Pascal's  triangle until all rows are printed
        {  
            printSpaces(spaceCount);        /// before printing any row print desired number of spaces 
            printRow(rowNumber);            /// this method prints a row and its argument decides which row to print 
            System.out.println();           /// after printing every row go to next line for next row
            rowNumber++;                    /// increase the row number because next time i want to print next row
            spaceCount--;                   /// decrease space count because next line needs less spaces before first element
        }

    }
    public static void main(String[] args)
    {
        PascalTriangle triangle = new PascalTriangle(20);   /// create an object and provide how many lines you want to print
        triangle.printPascalTriangle();                     /// call the prinitPascalTrianle method to print your Pascal Trianle
    } 

}
/**以下程序打印Pascal三角形
* 
*自下而上阅读以了解实现
*/
公共类PascalTriangle{
国际努弗罗斯;
PascalTriangle(int noOfRows)///用户必须提供要打印的Pascal三角形的行数
{
this.noOfRows=noOfRows;
}
private int factorial(int n)///此方法计算我们用于计算NCR的阶乘
{  
int-fact=1;

对于(int i=2;i而言,问题在于您的
fact()
函数。让我们将其分离并运行:

public class PascalTriangle {

    private int factorial(int n)
    {
        int fact = 1;

        for (int i = 2; i <= n; i++)
        {
            fact *= i;
        }

        return fact;
    }

    public static void main(String[] args)
    {
        PascalTriangle triangle = new PascalTriangle();

        for (int i = 1; i < 20; i++)
        {
            System.out.printf("%d = %d\n", i, triangle.factorial(i));
        }
    }
}
因为阶乘增长如此之快,您已经超过了
int
的容量。如果我们使用相同的代码并将所有
int
声明替换为
long
,您可以看到我们得到了更多的错误:

> java PascalTriangle
1 = 1
2 = 2
3 = 6
4 = 24
5 = 120
6 = 720
7 = 5040
8 = 40320
9 = 362880
10 = 3628800
11 = 39916800
12 = 479001600
13 = 6227020800
14 = 87178291200
15 = 1307674368000
16 = 20922789888000
17 = 355687428096000
18 = 6402373705728000
19 = 121645100408832000
> 

这似乎是一种昂贵的计算帕斯卡三角形的方法,因为每一行都可以使用加法从上一行派生出来。

非常感谢您,正如您所建议的,我将尝试使用上一行生成帕斯卡三角形的方法。。
> java PascalTriangle
1 = 1
2 = 2
3 = 6
4 = 24
5 = 120
6 = 720
7 = 5040
8 = 40320
9 = 362880
10 = 3628800
11 = 39916800
12 = 479001600
13 = 6227020800
14 = 87178291200
15 = 1307674368000
16 = 20922789888000
17 = 355687428096000
18 = 6402373705728000
19 = 121645100408832000
>