gpt4 book ai didi

java - 如何打印出数字之间的特定间距长度?

转载 作者:行者123 更新时间:2023-12-01 11:10:24 25 4
gpt4 key购买 nike

我想组织输出,所以我想做这样的事情

   1   2   3   4   5
........
91 92 93 94 95
........
101 102 103 104 105
etc....

所以我试图弄清楚如何将它们排列成每个数字之间正确的间距。

我的第一次尝试是像这样手动完成所有事情

System.out.print("  " + counter);

使用循环

最佳答案

阅读Formatting Numeric Print Output在文档中。

您可以使用System.out.format使用%<width>d在哪里替换 <width>根据您需要的宽度(确保所有内容都适合!):

System.out.format("%4d", number); // align to width of 4

要添加换行符,请使用 %n :

System.out.format("%4d%n", 1);
System.out.format("%4d%n", 12);
System.out.format("%4d%n", 123);

关于java - 如何打印出数字之间的特定间距长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32446192/

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