2016-11-30 52 views
5

我需要打印金字塔形狀的字符數組。我到目前爲止是這樣的:從字符中打印金字塔圖案

char[] chars = {'F', 'E', 'L', 'I', 'Z', ' ', 'A', 'N','I', 'V', 'E', 'R', 'S', 'A', 'R', 'I', 'O'}; 
    int length = chars.length; 


    for (int i = 1; i < length; i += 2) { 
    for (int j = 0; j < 9 - i/2; j++) 
    System.out.print(" "); 

    for (int j = 0; j < i; j++) 
    System.out.print(chars[j]); 

    System.out.print("\n"); 
} 
    for (int i = length; i > 0; i -= 2) { 
    for (int j = 0; j < 9 - i/2; j++) 
    System.out.print(" "); 

    for (int j = 0; j < i; j++) 
    System.out.print(chars[j]); 

    System.out.print("\n"); 

,它打印此:

  F 
      FEL 
     FELIZ 
     FELIZ A 
     FELIZ ANI 
     FELIZ ANIVE 
    FELIZ ANIVERS 
    FELIZ ANIVERSAR 
    FELIZ ANIVERSARIO 
    FELIZ ANIVERSAR 
    FELIZ ANIVERS 
     FELIZ ANIVE 
     FELIZ ANI 
     FELIZ A 
     FELIZ 
      FEL 
      F 

但我需要它開始從陣列中的字符打印。最終的結果是這樣的:

     I 
         NIV 
        ANIVE 
        ANIVER 
        Z ANIVERS 
        IZ ANIVERSA 
       LIZ ANIVERSAR 
       ELIZ ANIVERSARI 
       FELIZ ANIVERSARIO 
       ELIZ ANIVERSARI 
       LIZ ANIVERSAR 
        IZ ANIVERSA 
        Z ANIVERS 
        ANIVER 
        ANIVE 
         NIV 
         I 

任何幫助將不勝感激。

+0

也許環在串中的每個字符的每一行,但是要打印的空間,如果它不是在中間段要顯示? – qxz

+0

你可以得到中間索引,首先,它們具有相同的值,存儲在'midStart'和另一個'midEnd'中。然後增加'midStart'和遞減'midEnd' – raymelfrancisco

回答

5

你可以做同樣的事情,在下面的代碼中顯示。它將使用較少數量的for循環。我在代碼中添加了內聯註釋。

char[] chars = { 'F', 'E', 'L', 'I', 'Z', ' ', 'A', 'N', 'I', 'V', 'E', 'R', 'S', 'A', 'R', 'I', 'O' }; 
    int length = chars.length; 


    for (int line=0;line<=length;line++) { 
     //This will print upper part of pyramid 
     if(line < length/2){ 
      String output=""; 
      int middelVal=length/2; 
      for (int i = middelVal - line; i > 0; i--) { 
       output=output+" "; 
      } 
      for (int i = middelVal - line; i <= middelVal + line; i++) { 
       output=output+chars[i]; 
      } 
      System.out.println(output); 
     }else if(line > length/2){ 
      //This will print lower part of pyramid 
      String output=""; 
      int middelVal=length/2; 
      int nwNum = chars.length-line; 
      for (int i = middelVal - nwNum; i > 0; i--) { 
       output=output+" "; 
      } 
      for (int i = middelVal - nwNum; i <= middelVal + nwNum; i++) { 
       output=output+chars[i]; 
      } 
      System.out.println(output); 
     } 
    } 
2

這是從我的頭頂,但一個方法我想嘗試會是這樣的:

查找我的數組中的中間一個字符的索引。調用這個'startIndex'

對於每一行,我會找到長度除以2(舍入),這將是startIndex的起始偏移量,它決定了我的第一個字符是什麼。

比方說,我在第5行,你有「Z ANIVERS」。長度爲9.除以2(舍入),得到4.

在你的chars數組中,startIndex被計算爲8(17/2,向下舍入)。因此,我應該從字符數組中的char 4(它是startIndex - 4)開始打印,直到需要很多字符。

您還可以輕鬆地打印整行,而無需在循環中計算字符,因爲如果您知道所需的行長度和起始字符索引,那麼如果您的初始字符爲一弦。

最後,你也只能做一半的工作,因爲它看起來像你的上半部分是一樣的下半部分(建立結果字符串,然後將它們連接在一起,以打印出完整的結果)

編輯: 閱讀我的答案,你不需要實際'找到'行長度,因爲它從1開始,每次增加2,但原始方法仍然是相同的。

編輯2:

因爲每個人都將自己的編碼版本(和答案已被接受),我也可以添加以下礦井我說:

String sourceString = "FELIZ ANIVERSARIO"; // source of characters 
    String padding = "  "; // used to create the padding offset for each line 

    int middle = sourceString.length()/2; 

    // top and bottom halves of the output 
    String top = ""; 
    String bottom = ""; 

    for(int currentLength = 1; currentLength < sourceString.length(); currentLength += 2){ 
     String linePadding = padding.substring(currentLength/2, padding.length()); 

     // speical case here, in the given example by the post, the 4th line the offset is one less as the I characters do not line up in the middle 
     // 7 is used as the 4th line has a length of 7 
     if(currentLength == 7){ 
      linePadding = linePadding.substring(1, linePadding.length()); 
     } 

     top += linePadding + sourceString.substring(middle - (currentLength/2), middle + (currentLength/2) + 1) +"\n"; 
     bottom = linePadding + sourceString.substring(middle - (currentLength/2), middle + (currentLength/2) + 1) + "\n" + bottom; 
    } 

    System.out.println(top + sourceString +"\n"+ bottom); 

不知道這是故意的,但我在循環中添加了一個特殊的期望,我在用戶的例子中注意到第四行關閉了1(我不在那裏排隊)。如果我應該排隊,這是一個錯字,可以刪除,如果完全阻止。

3

獲取中間索引,將其存儲在兩個變量中將標記開始和結束字符打印的位置。

int mid = length/2; // left end 
int midLen = mid;  // right end 

循環將開始打印直至空間的左端mid,然後字符將被打印,直到右端midLen + 1。然後增加midLen進一步向右,並減小mid進一步向左。

for(int i = mid; mid > -1; mid--, midLen++) { 
    for(int s = 0; s < mid; s++) 
     System.out.print(" "); 

    for (int j = mid; j < midLen + 1; j++) { 
     System.out.print(chars[j]); 
    } 
    System.out.print("\n"); 
} 

要打印較低的金字塔,我們將開始從0指數length。這次,我們將mid增加到更遠的右邊,並且將midLen減少到更遠的左邊,截斷每行的字符串行直到中間字符。

mid = 0; 
midLen = length; 

for(int i = mid; midLen != length/2; mid++, midLen--) { 

    for(int s = 0; s < mid; s++) 
     System.out.print(" "); 

    for(int j = mid; j < midLen; j++) { 
     System.out.print(chars[j]); 
    } 
    System.out.print("\n"); 
} 

Test here

獎金:治理Java 8's new features。同樣的機制,我會離開你的檢查。

... 
static int mid; 
static int midLen; 
static String line = ""; 

public static void main(String[] args) { 
    char[] chars = {'F', 'E', 'L', 'I', 'Z', ' ', 'A', 'N','I', 'V', 'E', 'R', 'S', 'A', 'R', 'I', 'O'}; 

    mid = chars.length/2; 
    midLen = mid; 

    List<String> tri = new ArrayList<String>(); 
    IntStream.rangeClosed(0, mid) 
     .forEach(i -> { 
      Stream.of(chars).forEach(j -> { 
       IntStream.range(0, mid).forEach(x -> line += " "); 
       IntStream.rangeClosed(mid, midLen) 
        .forEach(x -> line += String.valueOf(chars[x])); 
       mid--; 
       midLen++; 
       tri.add(line); 
       line = ""; 
      }); 
     }); 

    tri.forEach(System.out::println); // print upper pyramid 
    Collections.reverse(tri); 
    tri.forEach(System.out::println); // print lower pyramid 
} 
... 
3

下面是使用2D陣列的另一種方法

char[] chars = { 'F', 'E', 'L', 'I', 'Z', ' ', 'A', 'N', 'I', 'V', 'E', 'R', 'S', 'A', 'R', 'I', 'O' }; 
    int size = chars.length; 
    int mid = size/2 + 1; 
    char[][] matrix = new char[size][size]; 
    for (char[] cs : matrix) { 
     Arrays.fill(cs, ' '); 
    } 
    for (int i = 0; i < size; i++) { 
     int l = Math.abs(mid - 1 - i); 
     int r = size - l; 
     for (int m = l; m < r; m++) { 
      matrix[i][m] = chars[m]; 
     } 
    } 
    for (char[] cs : matrix) { 
     System.out.println(cs); 
    } 

輸出

 I   
     NIV  
     ANIVE  
     ANIVER  
    Z ANIVERS  
    IZ ANIVERSA 
    LIZ ANIVERSAR 
ELIZ ANIVERSARI 
FELIZ ANIVERSARIO 
ELIZ ANIVERSARI 
    LIZ ANIVERSAR 
    IZ ANIVERSA 
    Z ANIVERS  
     ANIVER  
     ANIVE  
     NIV  
     I