Pascal's Triangle Format

前端 未结 8 1886
长发绾君心
长发绾君心 2020-12-03 08:38

The assignment is to create Pascal\'s Triangle without using arrays. I have the method that produces the values for the triangle below. The method accepts an integer for the

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 09:13

    public class HelloWorld{
    
         public static void main(String []args){
           int s=7;
           int k=1;
           int r;
    
           for(int i=1;i<=s;i++){
               int num=1;
               r=i;
               int col=0;
               for(int j=1;j<=2*s-1;j++){
    
                   if(j <= s-i)
                   System.out.print("  ");
                   else if(j >= s+i)
                   System.out.print("  ");
                   else{
                       if(k%2 ==0){
                           System.out.print("  ");
    
                       }
                       else{
                            if (col > 0) {
                                num = num * (r - col) / col;    
                            }
    
                        System.out.print(num+" ");
                           col++;
                       }
                   k++;
                   }
               }
               System.out.println("");
               k=1;
           }
    
    
    
    
         }
    }
    

提交回复
热议问题