如何使用java制作pascals三角形并且必须是字母格式

时间:2017-01-31 15:43:45

标签: java

所以我刚刚完成了一个关于帕斯卡三角形的脚本,并找到了如何配置它,但现在我遇到了问题,因为我试图将它放入一个字母的帕斯卡三角形中,例如。

                             A
                            A A
                           A B A
                          A C C A

我需要让这个三角形一直到z,我怎么用int(char ect? 这是正在运行的代码。

{
    int a, num;

    for (int i = 0; i <= 5; i++) 
    {
        num = 1;
        a = i + 1;

        for(int j=5;j>0;j--)
        { 
            if(j>i)
                System.out.print(" ");
        }

        for (int j = 0; j <= i; j++) 
        {
            if (j > 0) 
                num = num * (a - j) / j;   

            System.out.print(num + " ");
        }
        System.out.println();
    } 
}
    }

}

1 个答案:

答案 0 :(得分:-1)

您可以使用Switch Statement。使用新的String变量从 num 整数值手动保存字符/字母。然后显示字母值。

String alphabet = "";
switch(num){
            case 1 : alphabet ="A"; break;
            case 2 : alphabet ="B"; break;
            case 3 : alphabet ="C"; break;
            case 4 : alphabet ="D"; break;
            case 5 : alphabet ="E"; break;
            case 6 : alphabet ="F"; break;
            .
            .
            .
            case 22 : alphabet ="V"; break;
            case 23 : alphabet ="W"; break;
            case 24 : alphabet ="X"; break;
            case 25 : alphabet ="Y"; break;
            case 26 : alphabet ="Z"; break;
        } 

这是您的代码:

{
    int a, num;
    String alphabet = "";
    for (int i = 0; i <= 5; i++) 
    {
        num = 1;
        a = i + 1;

        for(int j=5;j>0;j--)
        { 
            if(j>i)
                System.out.print(" ");
        }

        for (int j = 0; j <= i; j++) 
        {
            if (j > 0) 
                num = num * (a - j) / j;   

            switch(num){
                case 1 : alphabet ="A"; break;
                case 2 : alphabet ="B"; break;
                case 3 : alphabet ="C"; break;
                case 4 : alphabet ="D"; break;
                case 5 : alphabet ="E"; break;
                case 6 : alphabet ="F"; break;
                case 7 : alphabet ="G"; break;
                case 8 : alphabet ="H"; break;
                case 9 : alphabet ="I"; break;
                case 10 : alphabet ="J"; break;
                case 11 : alphabet ="K"; break;
                case 12 : alphabet ="L"; break;
                case 13 : alphabet ="M"; break;
                case 14 : alphabet ="N"; break;
                case 15 : alphabet ="O"; break;
                case 16 : alphabet ="P"; break;
                case 17 : alphabet ="Q"; break;
                case 18 : alphabet ="R"; break;
                case 19 : alphabet ="S"; break;
                case 20 : alphabet ="T"; break;
                case 21 : alphabet ="U"; break;
                case 22 : alphabet ="V"; break;
                case 23 : alphabet ="W"; break;
                case 24 : alphabet ="X"; break;
                case 25 : alphabet ="Y"; break;
                case 26 : alphabet ="Z"; break;
            }            
            System.out.print(alphabet + " ");
        }
        System.out.println();
    } 
}

希望能帮助您理解