2017-01-11 4 views
-2

私はちょうど形式とスペースを取っているが、いずれか私にJavaの実際のパスカル三角形のプログラムで印刷する値を取得する単一の変更を教えてもらえます...私は自分の書かれたパスカルの論理の少しの変更を教えてくれる

/* 
* 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

2番目のコードに従ったと思います。ただ、そのサイトからの最初のコードは次のとおり

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 
関連する問題