java 如何在Java中的一行之间留一个空格?

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

How to make a space between a line in Java?

java

提问by Childish Yakuza

System.out.print("I have a question, can you assist me?");
System.out.println();
System.out.println("How can I make a gap between these two statements?");

I tried to use println(), thinking that it would create a blank line, but it didn't.

我尝试使用println(),认为它会创建一个空行,但事实并非如此。

采纳答案by userlond

Try:

尝试:

public class Main {

    public static void main(String args[]) {
        System.out.println("I have a question, can you assist me?\n");
        System.out.println("How can I make a gap between these two statements?");
    }
}

P.S. \nis newline separator and works ok at least on Windows machine. To achieve truly crossplatform separator, use one of methods below:

PS\n是换行符,至少在 Windows 机器上可以正常工作。要实现真正的跨平台分隔符,请使用以下方法之一:

System.out.print("Hello" + System.lineSeparator()); // (for Java 1.7 and 1.8)
System.out.print("Hello" + System.getProperty("line.separator")); // (Java 1.6 and below)

回答by Sam Estep

Here's what's going on.

这是怎么回事。

System.out.print("I have a question, can you assist me?");

You have now printed a bunch of characters, all on the same line. As you have used printand have not explicitly printed a newline character, the next character printed will also go onto this same line.

您现在已经在同一行上打印了一堆字符。由于您已经使用print并且没有明确打印换行符,因此打印的下一个字符也将进入同一行。

System.out.println();

This prints a newline character('\n'), which is notthe same as printing a blank line. Rather, it will cause the nextcharacter printed to go onto the line following the current one.

此打印一个换行字符'\n'),这是一样的打印一个空行。相反,它会导致打印的下一个字符进入当前字符的下一行。

System.out.println("How can I make a gap between these two statements?");

Since you just printed a newline character, this text will go onto the line directly following your "I have a question" line. Also, since you have called println, if you print anything right after this, it will go onto a new line instead of the same one.

由于您刚刚打印了一个换行符,因此该文本将直接放在“我有一个问题”行之后的行上。此外,由于您调用了println,如果您在此之后立即打印任何内容,它将转到新行而不是同一行。

To put a blank line between the two statements, you can do this (I know, I know, not entirely cross-platform, but this is just a very simple example):

要在两个语句之间放置一个空行,您可以这样做(我知道,我知道,不完全跨平台,但这只是一个非常简单的示例):

System.out.println("I have a question, can you assist me?");
System.out.println();
System.out.println("How can I make a gap between these two statements?");

Since you are now printing twonewline characters between the two lines, you'll achieve the gap that you wanted.

由于您现在在行之间打印两个换行符,因此您将获得所需的间隙。

回答by Stephen C

Beware that adding a bare "\n" to the string you are outputting is liable to make your code platform specific. For console output, this is probably OK, but if the file is read by another (platform native) application then you can get strange errors.

请注意,在您输出的字符串中添加一个空的“\n”可能会使您的代码平台特定。对于控制台输出,这可能没问题,但是如果文件被另一个(平台原生)应用程序读取,那么您可能会遇到奇怪的错误。

Here are some recommend approaches ... that should work on all platforms:

这里有一些推荐的方法......应该适用于所有平台:

  1. Just use println consistently:

     System.out.println("I have a question, can you assist me?");
     System.out.println();
     System.out.println("How can I make a gap?");
    

    Note: printlnuses the platform default end-of-line sequence.

  2. Use String.format:

     String msg = "I have a question, can you assist me?%n%nHow can " +
                  "I make a gap?%n";
     System.out.print(String.format(msg));
    

    Note: %nmeans the platform default end-of-line sequence.

    Note: there is a convenience printfmethod in the PrintWriterinterface that does the same thing as String.format

  3. Manually insert the appropriate end-of-line sequence into the string; see the end of @userlond's answer for examples.

  1. 只需一致地使用 println:

     System.out.println("I have a question, can you assist me?");
     System.out.println();
     System.out.println("How can I make a gap?");
    

    注意:println使用平台默认的行尾序列。

  2. 使用 String.format:

     String msg = "I have a question, can you assist me?%n%nHow can " +
                  "I make a gap?%n";
     System.out.print(String.format(msg));
    

    注:%n表示平台默认行尾顺序。

    注意:接口中有一个方便的printf方法PrintWriter,它的作用与String.format

  3. 手动插入适当的行尾序列到字符串中;有关示例,请参阅@userlond 答案的结尾。

回答by Aadol Rrashid

You can use the below code. By that method you can use as much line gap as you want. Just increase or decrease the number of "\n".

您可以使用以下代码。通过这种方法,您可以根据需要使用尽可能多的行距。只需增加或减少“\n”的数量。

System.out.print("Hello \n\n\n\n");
System.out.print("World");

回答by Harein Avishka Jayasekara

If you want to leave a single line space in java,you could use

如果你想在java中留下一个单行空间,你可以使用

System.out.println("");

But if you want to leave Multiple line spaces in java,you could use

但是如果你想在java中留下多行空格,你可以使用

System.out.println("/n/n/n/n");

which means that each '/n' represents a single line

这意味着每个 '/n' 代表一行

回答by Ronex

Use: system.out.println("\n");

使用:system.out.println("\n");

\n takes you to new line.

\n 带你到新行。

回答by nalzok

What about this one?

这个如何?

public class Main {
    public static void main(String args[]) {
        System.out.println("Something...");
        System.out.printf("%n"); // Not "\n"
        System.out.println("Something else...");
    }
}

"%n" should be crossplatform-friendly.

“%n”应该是跨平台友好的。