2017-01-11 1 views
-2

나는 형식과 간격을 가져 왔지만 어느 하나가 자바에서 실제 파스칼 삼각형 프로그램에서 인쇄하는 값을 얻기 위해 하나의 변화를 말할 수 있습니다 ...내 자신의 서면 파스칼 로직에서 약간의 변화를 말할 수 있습니다

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package patterns; 

/** 
* 
* @author Love Poet 
*/ 
public class p11 { 

    public static void main(String args[]) { 
     int row, col, count = 0; 
     for (row = 1; row <= 5; row++) { 
      for (col = 1; col <= 9; col++) { 
       if (((row + col) % 2 == 0) && (row + col >= 6)) { 
        System.out.print("* "); 
        count++; 
       } else if (count == row) { 
        break; 
       } else { 
        System.out.print(" "); 
       } 
      } 
      count = 0; 
      System.out.println(); 
     } 
    } 
} 

** 내 출력 : ** -

 * 
     * * 
    * * * 
    * * * * 
* * * * * 

내가 원하는 무엇 : -

   1    
     1  1   
     1  2  1  
    1  3  3  1 
1  4  6  4  1 
+1

내가 문제를 얻지 않는다합니다. 결과물이 예상대로 나오지 않습니까? – Thomas

+1

기호 대신 숫자를 인쇄 하시겠습니까? – Baby

+0

예 : - http://www.quickprogrammingtips.com/java/program-to-print-pascal-triangle-in-java.html –

답변

0

두 번째 코드를 따라 갔다고 생각합니다. 그냥 해당 사이트에서 첫 번째 코드를 따르

public class PascalTriangle { 
    public static void main(String[] args) { 
     int rows = 10; 
     for (int i = 0; i < rows; i++) { 
      int number = 1; 
      System.out.format("%" + (rows - i) * 2 + "s", ""); 
      for (int j = 0; j <= i; j++) { 
       System.out.format("%4d", number); 
       number = number * (i - j)/(j + 1); 
      } 
      System.out.println(); 
     } 
    } 
} 

출력 :

    1 
       1 1 
       1 2 1 
      1 3 3 1 
      1 4 6 4 1 
     1 5 10 10 5 1 
     1 6 15 20 15 6 1 
    1 7 21 35 35 21 7 1 
    1 8 28 56 70 56 28 8 1 
1 9 36 84 126 126 84 36 9 1 
관련 문제