2015-06-27 78 views
2

我寫我自己的邏輯來打印pascal三角形。有以下代碼:爲什麼我的pascal三角形程序沒有給出正確的格式?

import java.util.Scanner; 
public class pascal { 

    public static void main(String[] arg) { 
     Scanner sc = new Scanner(System.in); 
     System.out.print("please enter height of pascal triangle :"); 
     int Heightchk = 0, Height = sc.nextInt(); 
     if (Height > 0) { 
      MYarray PascalArray = new MYarray(); 
      PascalArray.setLength(Height * 2 - 1); 
      PascalArray.IntilizeA(PascalArray.A); 
      if (Height == 1) { 
       PascalArray.printArray(PascalArray.A); 
      } else { 
       while (Heightchk < Height) { 
        PascalArray.printArray(PascalArray.A); 
        Heightchk += 1; 
        if (Heightchk < Height) { 
         PascalArray.reSet(PascalArray.B); 
         PascalArray.setElements(PascalArray.A, PascalArray.B); 

         PascalArray.printArray(PascalArray.B); 
         Heightchk += 1; 
         if (Heightchk < Height) { 
          PascalArray.reSet(PascalArray.A); 
          PascalArray.setElements(PascalArray.B, PascalArray.A); 
         } 
        } 
       } 
      } 
     } else { 
      System.out.println("Can't Draw pascal Triangle of this Height"); 
     } 
    } 
} 

class MYarray { 
    String[] A, B; 
    void IntilizeA(String[] Array) { 
     Array[(Array.length - 1)/2] = "1"; 
    } 
    void setLength(int length) { 
     A = new String[length]; 
     B = new String[length]; 
     for (int i = 0; i < A.length; i++) { 
      A[i] = "\t"; 
      B[i] = "\t"; 
     } 
    } 
    void reSet(String[] Array) { 
     for (int i = 0; i < Array.length; i++) { 
      Array[i] = "\t"; 
     } 
    } 
    void printArray(String[] Array) { 
     for (String Element : Array) { 
      System.out.print(Element); 
     } 
     System.out.println(); 
     System.out.println(); 
     System.out.println(); 
    } 
    void setElements(String[] from, String[] to) { 
     for (int i = 0; i < from.length; i++) { 
      if (from[i] != "\t") { 
       if (to[i - 1] == "\t") { 
        to[i - 1] = "0"; 
       } 
       if (to[i + 1] == "\t") { 
        to[i + 1] = "0"; 
       } 
       to[i - 1] = String.valueOf(Integer.valueOf(to[i - 1]) + Integer.valueOf(from[i])); 
       to[i + 1] = String.valueOf(Integer.valueOf(to[i + 1]) + Integer.valueOf(from[i])); 
      } 
     } 
    } 
} 

邏輯,我申請工作超好,但仍然有一個元素的對齊問題。 它給下面的輸出:

please enter height of pascal triangle :5 
       1    


      1 1   


     1 2 1  


    1 3 3 1 

,而它的輸出應該是這樣的:

please enter height of pascal triangle :5 
       1    


      1  1   


     1  2  1  


    1  3  3  1 


1  4  6  4  1 

什麼是我的邏輯問題。我怎樣才能讓它正確?

+0

[帕斯卡三角正確的格式的Java(可能重複http://stackoverflow.com/questions/14371775/pascal -triangle-proper-formatting-java)和[Pascal三角形](http://stackoverflow.com/questions/15818341/pascal-triangle)和一些[230其他](http://stackoverflow.com/search q =帕斯卡三角形+ +是%3Aquestion)。 – Mogsdad

+0

@Mogsdad其實我寫了上面的代碼,但得到適當的輸出。 –

回答

1

你的代碼是絕對好的。但是當你用任何數字替換標籤\t時,你最終會刪除那個數組的縮進量。

例如,在您的第一個數組中 1是第四個元素,它是中心元素。這意味着它在Array的中心之前有4個選項卡。

而在第二陣,中心索引之前,你已經換成1的標籤之一,所以第2個數組的縮進,最終陣列的中心轉移到剩下1個選項卡。

這繼續下一個數組。所以你的格式不對。爲了解決這個問題,請不要將tab換成數字,而是將數字追加到\t。這將確保您的縮進是完整的。

下面是更新的代碼。此外,我更新你的代碼遵循標準命名約定

import java.util.Scanner; 
public class Pascal { 

    public static void main(String[] arg) { 
     Scanner sc = new Scanner(System.in); 
     System.out.print("plese enter height of pascal tringle :"); 
     int heightchk = 0, height = sc.nextInt(); 
     if (height > 0) { 
      MyArray pascalArray = new MyArray(); 
      pascalArray.setLength(height * 2 - 1); 
      pascalArray.IntilizeA(pascalArray.a); 
      if (height == 1) { 
       pascalArray.printArray(pascalArray.a); 
      } else { 
       while (heightchk < height) { 
        pascalArray.printArray(pascalArray.a); 
        heightchk += 1; 
        if (heightchk < height) { 
         pascalArray.reSet(pascalArray.b); 
         pascalArray.setElements(pascalArray.a, pascalArray.b); 

         pascalArray.printArray(pascalArray.b); 
         heightchk += 1; 
         if (heightchk < height) { 
          pascalArray.reSet(pascalArray.a); 
          pascalArray.setElements(pascalArray.b, pascalArray.a); 
         } 
        } 
       } 
      } 
     } else { 
      System.out.println("Can't Drow pascal Tringle of this Height"); 
     } 

     sc.close(); 
    } 
} 

class MyArray { 
    String[] a, b; 
    void IntilizeA(String[] array) { 
     array[(array.length - 1)/2] = "\t1"; 
    } 
    void setLength(int length) { 
     a = new String[length]; 
     b = new String[length]; 
     for (int i = 0; i < a.length; i++) { 
      a[i] = "\t"; 
      b[i] = "\t"; 
     } 
    } 
    void reSet(String[] array) { 
     for (int i = 0; i < array.length; i++) { 
      array[i] = "\t"; 
     } 
    } 
    void printArray(String[] array) { 
     for (String element : array) { 
      System.out.print(element); 
     } 
     System.out.println(); 

    } 
    void setElements(String[] from, String[] to) { 
     for (int i = 0; i < from.length; i++) { 
      if (from[i] != "\t") { 
       if (to[i - 1] == "\t") { 
        to[i - 1] = "0"; 
       } 
       if (to[i + 1] == "\t") { 
        to[i + 1] = "0"; 
       } 
       to[i - 1] = "\t"+String.valueOf(Integer.valueOf(to[i - 1].trim()) + Integer.valueOf(from[i].trim())); 
       to[i + 1] = "\t"+String.valueOf(Integer.valueOf(to[i + 1].trim()) + Integer.valueOf(from[i].trim())); 
      } 
     } 
    } 
} 

輸出

   1    
      1  1   
     1  2  1  
    1  3  3  1 
1  4  6  4  1 
0

既然你知道身高,你可以推斷出最後一行的寬度。其中一半是中間的,這是第一行應該從水平開始的地方。對於每條附加線,在中間開始之前多出兩個空格。

+0

抱歉,但我不明白@Trow方式 –

相關問題