如何打印垂直对齐文本 [英] How to print vertically aligned text
本文介绍了如何打印垂直对齐文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要打印以下格式的输出文件。
I want to print an output of the following format in a file..
1 Introduction 1
1.1 Scope 1
1.2 Relevance 1
1.2.1 Advantages 1
1.2.1.1 Economic 2
1.2.2 Disadvantages 2
2 Analysis 2
我不能得到的页码在一条线上垂直对齐。如何做到这一点?
I cannot get the page numbers to align vertically in a line. How to do this??
推荐答案
您需要左对齐的第一列和右对齐的第二列。
You need to left-justify the first column, and right-justify the second column.
下面是一个例子:
String[] titles = {
"1 Introduction",
" 1.1 Scope",
" 1.2 Relevance",
" 1.2.1 Advantages",
" 1.2.1.1 Economic",
" 1.2.2 Disadvantages",
"2 Analysis",
};
for (int i = 0; i < titles.length; i++) {
System.out.println(String.format("%-30s %4d",
titles[i],
i * i * i // just example formula
));
}
此照片(因为看到ideone.com ):
1 Introduction 0
1.1 Scope 1
1.2 Relevance 8
1.2.1 Advantages 27
1.2.1.1 Economic 64
1.2.2 Disadvantages 125
2 Analysis 216
格式% - 30秒%4D
左对齐,( -
标志)先用宽30说法,右对齐,宽度为4的第二个参数。
The format %-30s %4d
left-justifies (-
flag) the first argument with width of 30, and right-justifies the second argument with width of 4.
- <一个href="http://java.sun.com/javase/6/docs/api/java/util/Formatter.html"><$c$c>java.util.Formatter$c$c>
这篇关于如何打印垂直对齐文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文