gpt4 book ai didi

java - 如何打印垂直对齐的文本

转载 作者:搜寻专家 更新时间:2023-10-31 08:14:20 24 4
gpt4 key购买 nike

我想在文件中打印以下格式的输出..

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

我无法让页码在一行中垂直对齐。如何做到这一点??

最佳答案

您需要将第一列左对齐,将第二列右对齐。

这是一个例子:

    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
));
}

这会打印 ( as seen on 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

格式 %-30s %4d 左对齐(- 标志)宽度为 30 的第一个参数,右对齐宽度为 4 的第二个参数.

API 链接

关于java - 如何打印垂直对齐的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3177697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com