如何在java中打印2个换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23980834/
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
How to print 2 newlines in java?
提问by user2489526
I know that
我知道
for
example
is printed by: System.out.print("for" + "\n" + "example")
But what do I do when I want to print 2 blank lines instead of 1?
Like this:
打印者:System.out.print("for" + "\n" + "example")
但是当我想打印 2 个空白行而不是 1 个时该怎么办?像这样:
for
example
I tried System.out.print("for" + "\n" + "\n" + "example")
but it still printed 1 blank line
我试过了,System.out.print("for" + "\n" + "\n" + "example")
但它仍然打印了 1 个空行
回答by BobTheBuilder
You can use:
您可以使用:
System.out.println("for");
System.out.println();
System.out.println();
System.out.println("example");
To print what you need.
打印你需要的东西。
System.out.println();
just prints an empty line.
只打印一个空行。
As for your solution, you actually need 3new line characters (one after "for" and two empty lines).
至于您的解决方案,您实际上需要3 个新行字符(一个在“for”之后和两个空行)。
Your first statement is wrong, as:
你的第一句话是错误的,因为:
System.out.print("for" + "\n" + "example")
will print:
将打印:
for
example
回答by Jens
System.out.print("for" + "\n\n\n" + "example")
should solve your problem. The first "\n"
is for ending "for"
and then two blank lines
System.out.print("for" + "\n\n\n" + "example")
应该可以解决您的问题。第一个"\n"
是结束"for"
,然后是两个空行
or
或者
System.out.println("for");
System.out.println();
System.out.println();
System.out.println("example");
回答by Fre_d
Using System.getProperty("line.separator")
should return the operator you are looking for. My guess is that this property depends from OS'es. You can use this to print two separate lines:
UsingSystem.getProperty("line.separator")
应该返回您正在寻找的运算符。我的猜测是这个属性取决于操作系统。您可以使用它来打印两个单独的行:
System.out.prinln("line 1"+System.getProperty("line.separator")+"line 2");
This will print:
这将打印:
line 1
line 2
回答by moe
There are different ways of printing two newlines:
打印两个换行符有不同的方法:
Write an empty print statement twice or more:
System.out.println(); System.out.println();
System.out.print( "\n\n\n" )
Use a loop in case you want to use a single
println
and even without using/n
:public void foo(int n) { if (n > 3) return; println(currNum); foo(n+1); }
写一个空的打印语句两次或更多次:
System.out.println(); System.out.println();
System.out.print( "\n\n\n" )
如果您想使用单个
println
甚至不使用循环,请使用循环/n
:public void foo(int n) { if (n > 3) return; println(currNum); foo(n+1); }