Pascal's Triangle Format

前端 未结 8 1847
长发绾君心
长发绾君心 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:10

    You need to print the spaces (like others have mentioned) and also as this is homework I'm leaving it to you but you might want to look at this handy little function

    System.out.printf();
    

    Here is a handy reference guide

    Also note that you will need to take into account that some numbers are more than 1 digit long!

    0 讨论(0)
  • 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;
           }
    
    
    
    
         }
    }
    
    0 讨论(0)
提交回复
热议问题