2015-06-27 50 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方式 –

相关问题