public static void main(String[] args) {
String[] names = {"Regina Kareem Obrien","Gretchen Brewer","Marty Cox","Roxie Blankenship","Stacey Trujillo","Ahmad James","Jordan Sykes","Frankie Hodge","Marguerite Doyle","Wilmer Baldwin"};
String[] address = {"Avenida Ressano Garcia","Rua Roberto Duarte Silva","Avenida de Roma","Rua da Betesga","Rua Damasceno Monteiro","Rua de São Julião","Rua de São Mamede","Rua do Comércio","Rua do Salitre","Rua dos Bacalhoeiros"};
String[] dob = {"1999/8/26","2008/5/24","2002/10/7","2010/1/26","2005/11/21","2007/4/9","1999/8/26","2008/5/24","2002/10/7","2010/1/26"};
int[] phone = {607544367,604903566,411315929,261809725,538236027,442128744,117255969,241273904,666757780,316948768};
for (int i = 0; i < 10; i++){
System.out.println("Name: " + names[i] + " Address: " + address[i] + " DOB: " + dob[i] + " Phone: " + phone[i]);
}
}
这样打印出来:
Name: Regina Kareem Obrien Address: Avenida Ressano Garcia DOB: 1999/8/26 Phone: 607544367
Name: Gretchen Brewer Address: Rua Roberto Duarte Silva DOB: 2008/5/24 Phone: 604903566
Name: Marty Cox Address: Avenida de Roma DOB: 2002/10/7 Phone: 411315929
Name: Roxie Blankenship Address: Rua da Betesga DOB: 2010/1/26 Phone: 261809725
Name: Stacey Trujillo Address: Rua Damasceno Monteiro DOB: 2005/11/21 Phone: 538236027
Name: Ahmad James Address: Rua de São Julião DOB: 2007/4/9 Phone: 442128744
Name: Jordan Sykes Address: Rua de São Mamede DOB: 1999/8/26 Phone: 117255969
Name: Frankie Hodge Address: Rua do Comércio DOB: 2008/5/24 Phone: 241273904
Name: Marguerite Doyle Address: Rua do Salitre DOB: 2002/10/7 Phone: 666757780
Name: Wilmer Baldwin Address: Rua dos Bacalhoeiros DOB: 2010/1/26 Phone: 316948768
是否可以格式化输出格式化的方式?
我想要这样的事情:
Name: Regina Kareem Obrien Address: Avenida Ressano Garcia DOB: 1999/8/26 Phone: 607544367
Name: Gretchen Brewer Address: Rua Roberto Duarte Silva DOB: 2008/5/24 Phone: 604903566
Name: Marty Cox Address: Avenida de Roma DOB: 2002/10/7 Phone: 411315929
Name: Roxie Blankenship Address: Rua da Betesga DOB: 2010/1/26 Phone: 261809725
Name: Stacey Trujillo Address: Rua Damasceno Monteiro DOB: 2005/11/21 Phone: 538236027
Name: Ahmad James Address: Rua de São Julião DOB: 2007/4/9 Phone: 442128744
Name: Jordan Sykes Address: Rua de São Mamede DOB: 1999/8/26 Phone: 117255969
Name: Frankie Hodge Address: Rua do Comércio DOB: 2008/5/24 Phone: 241273904
Name: Marguerite Doyle Address: Rua do Salitre DOB: 2002/10/7 Phone: 666757780
Name: Wilmer Baldwin Address: Rua dos Bacalhoeiros DOB: 2010/1/26 Phone: 316948768
答案 0 :(得分:2)
使用System.out.format(...)
。例如:
System.out.format("%10s%15s%20s", s1, s2, s3)
将s1
字符串填充为10,将s2
字符串填充为15,将s3
字符串填充为20个字符。
看看Formatter javadoc。它彻底解释了这个话题。
答案 1 :(得分:2)
解决您的问题:
System.out.println(String.format("Name: %-20s Address: %-30s DOB: %-10s Phone: %s",
names[i], address[i], dob[i], phone[i]));
use - 用于带空格的右边填充
答案 2 :(得分:1)
是的,请使用System.out.printf
,format string使用{{3}}指定并格式化其输出。
您可以使用带有%s
参数的宽度说明符来生成正确的格式,例如:%-30s
。 -
很重要,它会使文字保持对齐。例如
System.out.printf("Name: %-30s, Address: %-30s...", names[i], address[i]);
玩弄它来按照你喜欢的方式进行游戏。
答案 3 :(得分:1)
我猜\t
应该对你有所帮助。
试着玩这个标签,让我们这样说:
System.out.println("Name: " + names[i] + "\t Address: " + address[i] + "\t DOB: " + dob[i] + "\t Phone: " + phone[i]);
答案 4 :(得分:0)
您可以使用标签,或查看JCurses
格式化控制台输出。