-2

我有3个这样的数组:

String[] monthName = { "January", "February", "March",  
                       "April", "May", "June", "July",  
                       "August", "September", "October",  
                       "November", "December" };  

Double[] temperature = {68.1,69.1,72.4,75.7,79.6,82.4,83.7,83.6,82.4,78.8,74.4,69.9};
Double[] precipitation = {1.9,2.1,2.6,3.4,5.5,8.5,5.8,8.6,8.4,6.2,3.4,2.2};

我怎么能像这样打印这个值:

                   Climate Data
           Location:

Month      Temperature (F)    Precipitation (in.)
*************************************************
 Jan.          51.8                  5.4
 Feb.          54.8                  4.6
 Mar.          61.1                  6.5
 Apr.          66.4                  3.6
 May           74.4                  5.0
 Jun.          80.4                  6.9
 Jul.          82.4                  8.0
 Aug.          82.1                  7.0
 Sep.          78.9                  5.0
 Oct.          69.1                  3.3
 Nov.          60.4                  3.9
 Dec.          53.7                  4.1
*************************************************           
4

1 回答 1

1

查看String.format()方法。

这是一个填充两个字符串的示例,使它们占据 12 列并左对齐:

String paddedOne = String.format("%-12s", "Hello");
String paddedTwo = String.format("%-12s", "Goodbye");
System.out.println(paddedOne + paddedTwo);

输出:

Hello       Goodbye
于 2012-11-20T00:29:05.227 回答