2016-11-30 88 views
0

好吧,這可能是一個非常基本的問題,但我對我的生活無法弄清楚Java輸出格式,我只是沒有任何練習。我正在構建一個術語文檔矩陣,用於統計一系列句子中某些單詞的出現次數。矩陣出來罰款與下面的代碼帶標籤的格式化矩陣

for(int i=0; i<rows; i++){ 
      for(int j=0; j<columns; j++){ 
       System.out.print(String.format("%3s", matrix[i][j])); 
      } 
      System.out.println(""); 
} 

1.4.3本:

0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 0 
    0 0 0 0 0 0 1 0 0 0 0 2 0 0 1 0 0 0 0 1 0 2 0 0 0 1 0 0 0 0 0 0 0 
    0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 3 0 0 1 0 0 0 0 0 
    0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 
    0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 
    0 1 1 0 0 0 0 0 0 0 0 2 0 0 1 0 1 0 0 1 0 2 0 0 0 0 1 0 0 0 0 0 0 
    1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 
    0 0 0 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 
    0 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 
    0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 2 0 0 3 0 0 0 0 0 
    0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 0 0 
    0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 
    0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 1 1 0 0 0 0 1 0 0 0 0 0 0 
    0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 
    1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 
    0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 2 0 1 0 1 0 1 0 0 0 0 0 0 0 
    0 0 0 0 1 1 0 0 0 2 0 0 1 1 0 1 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 1 

不過,我想補充的行和列標識符,使它看起來更像是這個(列標題來自一個字符串數組並會有20句):

   applianc, around, autonom, averag, bath, bedroom, car, charg, drive, eat, home, hour, hous, includ, kilomet, kitchen, lap, larg, live, mile, over, per, pet, road, room, second, sedan, size, test, up, updat, veri, water 
Sentence 1: 

Sentence 2: 

Sentence 3: 
. 
. 
. 

通過行和列排隊,使他們很容易辨認,或許與電網或東西作爲分隔符。我對如何實現這一目標毫無頭緒。

回答

0

下你的榜樣,這樣的事情應該工作

String[] columns = {"applianc", " around", " autonom", " averag", " bath", " bedroom", " car", " charg", " drive", " eat", " home", " hour", " hous", " includ", " kilomet", " kitchen", " lap", " larg", " live", " mile", " over", " per", " pet", " road", " room", " second", " sedan", " size", " test", " up", " updat", " veri", " water"}; 
int[][] matrix = {{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 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0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0}, 
     {0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0}, 
     {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0}, 
     {0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
     {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, 
     {0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 
     {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0}, 
     {0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 2, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0}, 
     {0, 0, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1}}; 


System.out.print(String.format("%" + ("sentence ".length() + ("" + matrix.length).length() + 2) + "s"," ")); 

for(int i=0; i<columns.length; i++){ 
    System.out.print(columns[i]); 
} 

System.out.println(""); 

for(int i=0; i<matrix.length; i++) { 
    System.out.print("sentence " + String.format("%" + ("" + matrix.length).length() + "S", (i + 1)) + ": "); 

    for(int j=0; j<matrix[i].length; j++){ 
     System.out.print(String.format("%" + columns[j].length() + "s", matrix[i][j])); 
    } 

    System.out.println(""); 
} 

輸出:

   applianc around autonom averag bath bedroom car charg drive eat home hour hous includ kilomet kitchen lap larg live mile over per pet road room second sedan size test up updat veri water 
sentence 1:  0  0  1  0 0  0 0  0  0 0 0 1 0  0  0  0 0 0 0 1 0 1 0 1 0  0  1 0 0 1  0 0  0 
sentence 2:  0  0  0  0 0  0 1  0  0 0 0 2 0  0  1  0 0 0 0 1 0 2 0 0 0  1  0 0 0 0  0 0  0 
sentence 3:  0  0  0  0 1  1 0  0  0 1 1 0 0  0  0  1 0 1 0 0 0 0 0 0 3  0  0 1 0 0  0 0  0 
sentence 4:  0  1  0  0 0  0 0  1  0 0 0 0 0  0  1  0 0 0 0 1 0 0 0 1 0  0  0 0 1 0  0 0  0 
sentence 5:  0  0  0  0 0  1 0  0  0 0 1 0 0  0  0  0 0 1 0 0 0 0 0 0 2  0  0 0 0 0  0 0  0 
sentence 6:  0  0  0  0 0  0 1  1  0 0 0 0 0  0  1  0 0 0 0 1 0 1 0 0 0  0  0 0 0 1  0 0  0 
sentence 7:  0  1  1  0 0  0 0  0  0 0 0 2 0  0  1  0 1 0 0 1 0 2 0 0 0  0  1 0 0 0  0 0  0 
sentence 8:  1  0  0  0 0  0 0  0  0 0 0 0 0  1  0  0 0 0 0 0 0 0 1 0 0  0  0 0 0 0  0 0  1 
sentence 9:  0  0  0  0 1  1 0  0  0 0 1 0 1  0  0  0 0 0 0 0 0 0 0 0 1  0  0 0 0 0  0 0  0 
sentence 10:  0  0  0  0 0  0 0  0  0 0 0 0 0  0  0  0 0 0 0 0 0 0 1 0 0  0  0 0 0 0  0 0  0 
sentence 11:  0  0  1  0 0  0 1  0  1 0 0 0 0  0  1  0 0 0 0 0 0 0 0 1 0  0  0 0 1 0  0 0  0 
sentence 12:  0  0  0  0 0  2 0  0  0 0 0 0 0  0  0  0 0 1 0 0 0 0 0 0 2  0  0 3 0 0  0 0  0 
sentence 13:  0  0  1  0 0  0 0  0  0 0 0 0 0  0  1  0 0 0 0 1 1 0 0 0 0  0  0 0 0 0  0 0  0 
sentence 14:  0  0  0  0 0  0 0  0  0 0 0 0 1  0  0  1 0 0 0 0 0 0 0 0 0  0  0 0 0 2  2 0  0 
sentence 15:  0  0  1  0 0  0 1  0  0 0 0 0 0  0  0  0 0 0 0 0 0 0 0 1 0  0  0 0 0 0  0 0  0 
sentence 16:  0  0  0  1 0  0 0  0  0 0 0 0 0  0  0  0 0 0 0 2 1 1 0 0 0  0  1 0 0 0  0 0  0 
sentence 17:  0  0  0  0 1  1 0  0  0 0 1 0 0  0  0  0 0 0 0 0 0 0 0 0 1  0  0 0 0 0  0 0  0 
sentence 18:  1  0  0  0 0  0 0  0  0 1 0 0 1  0  0  2 0 0 0 0 0 0 0 0 0  0  0 0 0 1  1 0  0 
sentence 19:  0  1  0  1 0  0 1  0  0 0 0 1 0  0  0  0 1 0 0 2 0 1 0 1 0  1  0 0 0 0  0 0  0 
sentence 20:  0  0  0  0 1  1 0  0  0 2 0 0 1  1  0  1 0 0 0 0 0 0 0 0 3  0  0 0 0 0  0 0  1 
+0

實際上它甚至可能是更好的具有相同的列寬,即相同的字符數,使「applianc 「和」向上「具有相同的寬度 – bahamout