java System.out.printf( "%-15s%03d\n", s1, x) 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37780027/
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-11-03 02:47:39  来源:igfitidea点击:

What does System.out.printf( "%-15s%03d\n", s1, x) do?

javaprintf

提问by Django

String s1=sc.next();
int x=sc.nextInt();
System.out.printf( "%-15s%03d\n", s1, x);

Can someone explain the part inside the 'printf' braces?

有人可以解释'printf'大括号内的部分吗?

回答by Leon

This is Java's formatter syntax. You can find more about it here. In your case, you have 2 parameters, that get formatted.

这是 Java 的格式化程序语法。您可以在此处找到更多相关信息。在您的情况下,您有 2 个已格式化的参数。

First s1which is formatted using %-15s. The %means that what follows is an argument that will be formatted. Then follows a -resulting in left alignment. 15fills the string up to a length of 15 characters (adding spaces at the end). Finally the smeans, that you are formatting a string.

首先s1是使用%-15s. 这%意味着接下来是一个将被格式化的参数。然后跟随-导致左对齐。15将字符串填充到 15 个字符的长度(在末尾添加空格)。最后的s意思是,您正在格式化一个字符串。

Second xwhich is formatted using %03d. Here the 0is the fill character, meaning that, if necessary, zeros are added. The 3is again the width, meaning the fill character 0is added as many times as necessary to make it 3 digits long (this time at the beginning). Finally dmeans, that a integer is formatted.

第二个x使用%03d. 这里的0是填充字符,这意味着,如有必要,添加零。该3又是宽度,这意味着填充字符0添加为根据需要多次,使其3个数字长(此时开头)。最后d意味着,一个整数被格式化。

回答by Muhammad Tahsin Amin

"%-15s" means that within 15 blank space, the String "s1" will be filled in the left. (fill in the blanks from the left) "%03d" means that within 3 0s, the integer"x" will be filled in the right.(fill in the zeros from the right).

"%-15s" 表示在 15 个空格内,字符串 "s1" 将填充在左边。(从左边开始填空)“%03d”表示在3个0s内,整数“x”会填在右边。(从右边开始填零)。

This can only be done by using printf method.

这只能通过使用 printf 方法来完成。

回答by 28rox

This is an syntax of String formatted and it can be only done with printf method in java.
as we already know we use "s"for string and "d"for number and here we can see in the syntax they have used "-"(space/white-spaces) followed by digit "15"it means the output will be with 15 white-spaces for a given string (from the left side) and in the same syntax we can see "03d", meaning that, if necessary, zeros are added.

这是一种String格式的语法,只能用java中的printf方法来完成。
正如我们已经知道的那样,我们使用“s”表示字符串,“d”表示数字,在这里我们可以看到他们使用“-”(空格/空格)后跟数字“15”的语法,这意味着输出将是给定字符串有 15 个空格(从左侧),在相同的语法中,我们可以看到"03d",这意味着,如有必要,添加零。

回答by Все Едно

Best tutorial for printfout there

最佳教程printf在那里

The basics are: %sis looking for the first string as an argument witch it can find, %dfor the first int, and 03before %dis a modifier on how exactly you want to print the decimal number.

基本原理是:%s正在寻找第一个字符串作为它可以找到的参数,%d对于第一个 int,03before%d是一个修饰符,用于确定要打印十进制数的准确程度。