2016-02-12 37 views
0

我已經遇到了一個我一直在處理的個人Java項目中的一個難題。我想以表格的形式打印字符串的二維數組。不是自己的字符串,而是行列標籤(想想Microsoft Excel)。這是我設想的最終產品,星號代表字符串應該去的地方。打印二維數組作爲網格? (Java)

| A | B | C | D | E | F | G | 
----+---------+---------+---------+---------+---------+---------+---------+ 
    1 | * | * | * | * | * | * | * | 
----+---------+---------+---------+---------+---------+---------+---------+ 
    2 | * | * | * | * | * | * | * | 
----+---------+---------+---------+---------+---------+---------+---------+ 
    3 | * | * | * | * | * | * | * | 
----+---------+---------+---------+---------+---------+---------+---------+ 
    4 | * | * | * | * | * | * | * | 
----+---------+---------+---------+---------+---------+---------+---------+ 
    5 | * | * | * | * | * | * | * | 
----+---------+---------+---------+---------+---------+---------+---------+ 
    6 | * | * | * | * | * | * | * | 
----+---------+---------+---------+---------+---------+---------+---------+ 
    7 | * | * | * | * | * | * | * | 
----+---------+---------+---------+---------+---------+---------+---------+ 
    8 | * | * | * | * | * | * | * | 
----+---------+---------+---------+---------+---------+---------+---------+ 
    9 | * | * | * | * | * | * | * | 
----+---------+---------+---------+---------+---------+---------+---------+ 
10 | * | * | * | * | * | * | * | 
----+---------+---------+---------+---------+---------+---------+---------+ 

我知道,這將使用一個嵌套前饋迴路,並且所述邏輯過程將是把該字符串值在內部循環,如「例如[i] [j]」型格式。我只是很困惑,以至於如何以正確的格式圍繞單元格進行設計,將每個字符串限制爲10個字符,例如Excel在縮小時限制其單元格大小的方式。我是否使用子字符串?我是否使用printf來正確地分隔第十行?

任何指針都非常感謝,我從來沒有像以前這樣難過。

+2

請張貼您的圖片。 –

+1

使用printf打印單行。做一個方法。從那裏開始。 –

回答

1

第一行應該很容易,假設您不超過26列,即列名僅爲AZ

偶數行均爲----+的導入,然後是---------+的columnCount重複。

奇數行(除第一個)是999 |的導入符號,其中999是一個右對齊的行號,具有前導空格。這可以用printf()String.format()完成,格式字符串爲
"%3d |"

在導入之後是一個字符串值的columnCount重複,修剪和中心對齊爲9個字符,然後是|

爲了中心對準到9個字符,執行以下操作:

  • 如果長度> 9,修剪至9(是的,使用substring())。
  • 否則,計算所需的空間,即spacesTotal = 9 - trimmedLength
    計算左側空格:spaceBefore = spacesTotal/2
    計算右側的空格:spaceAfter = spacesTotal - spaceBefore
    通過這樣做,像5這樣的奇數個空格變成2之前和3之後。
    現在打印spaceBefore空格,(修剪的)文本值,然後spaceAfter空格和|
+0

非常感謝Andreas。這回答了我所有的問題。我走了,謝謝你!欣賞它。 –

0
public static void printStringGrid(String[][] array){ 
    System.out.print(" |"); 
    for (int i = 0; i < array[0].length; i++){ 
     System.out.print(" "); 
     System.out.print((char)('A' + i)); 
     System.out.print(" |"); 
    } 
    System.out.println(); 
    for (int i = 0; i < array.length; i++){ 
     System.out.print("----+"); 
     for (int j = 0; j < array[0].length; j++){ 
      System.out.print("---------+"); 
     } 
     System.out.println(); 
     System.out.print(" " + (i + 1) + " |"); 
     for (int j = 0; j < array[0].length; j++){ 
      if (array[i][j].length() < 10){ 
       int spaces = (9 - array[i][j].length())/2; 
       for (int k = 0; k < spaces; k++){ 
        System.out.print(" "); 
       } 
       System.out.print(array[i][j]); 
       for (int k = 0; k < (9 - array[i][j].length()) - spaces; k++){ 
        System.out.print(" "); 
       } 
      } 
      else{ 
       System.out.print(array[i][j].substring(0, 9)); 
      } 
      System.out.print("|"); 
     } 
     System.out.println(); 
    } 
}