java用printf创建空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19961671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:51:18  来源:igfitidea点击:

java creating blank spaces with printf

javaprintfspaces

提问by user2967353

When I run my program, I get this:

当我运行我的程序时,我得到了这个:

run:
                                            Heat Index: Key West, Florida



            Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
____________________________________________________________________________________________________________________________________

Temperature:      70.3      70.8      73.8      77.0      80.7      83.4      84.5      84.4      83.4      80.2      76.3      72.0
   Humidity:      69.0      67.0      66.0      64.0      66.0      69.0      67.0      67.0      70.0      69.0      69.0      70.0BUILD SUCCESSFUL (total time: 0 seconds)

I want to display this so that the months align with each column of numbers, like Jan aligns with the first column of numbers and so on. I know that there are 7 blank spaces between each column of number if the months are aligned. The thing is, I don't know how to create blank spaces, like blank spaces so the months won't show up on top of the Temperature: heading, with printf. Any help will be greatly appreciated.

我想显示这个,以便月份与每一列数字对齐,比如 Jan 与第一列数字对齐等等。我知道如果月份对齐,每列数字之间有 7 个空格。问题是,我不知道如何创建空格,比如空格,这样月份就不会出现在温度的顶部:标题,带有 printf。任何帮助将不胜感激。

回答by A4L

You need to use System.out.printf()and passing the appropriate flags for the width, see herefor the syntax for formatting.

您需要使用System.out.printf()并为宽度传递适当的标志,请参阅此处的格式语法。

String[] months = 
{"Jan",  "Feb",  "Mar",   "Apr",  "May", "Jun",   "Jul",  "Aug",
 "Sep",  "Oct",  "Nov",  "Dec"};
Double[] temper = 
    {70.3, 70.8, 73.8, 77.0, 80.7, 83.4, 84.5, 84.4, 83.4, 80.2, 76.3, 72.0};
Double[] humid = 
    {69.0, 67.0, 66.0, 64.0, 66.0, 69.0, 67.0, 67.0, 70.0, 69.0, 69.0, 70.0};

System.out.printf("            %7s%7s%7s%7s%7s%7s%7s%7s%7s%7s%7s%7s\n",
    (Object[])months);
System.out.printf("________________________________________"
    + "________________________________________________________\n");
System.out.printf("Temperature:%7.1f%7.1f%7.1f%7.1f%7.1f"
    + "%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f\n", 
    (Object[])temper);
System.out.printf("   Humidity:%7.1f%7.1f%7.1f%7.1f%7.1f"
    + "%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f\n",
    (Object[])humid);

回答by johnsoe

This will print exactly four spaces plus the three taken up by the month. Also, if you just want to print Strings you don't really need printf. Below are two options.

这将正好打印四个空格加上月份占用的三个空格。另外,如果你只是想打印字符串,你真的不需要 printf。下面是两个选项。

System.out.printf("    %s", month);

or

或者

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

I would also recommend coming up with a way to do this without hard coding the spaces. Maybe set up a loop to print spaces which can change the width.

我还建议想出一种方法来做到这一点,而无需对空间进行硬编码。也许设置一个循环来打印可以改变宽度的空间。

回答by subash

try this.. you can specify spaces between each print..

试试这个..你可以在每个打印之间指定空格..

        String[] day = {"Sun", "Mon", "Tues", "Friday","Saturday"};
        for(String s :day){
            System.out.printf("%15s", s);
        }

回答by user6652577

try this System.out.println("\n");

试试这个 System.out.println("\n");