如何在Java中制造行间距?

3
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?");

我尝试使用println(),以为它会创建一行空白,但实际上并没有。


3
System.out.println(""); or System.out.print("I have a question, can you assist me?\n"); - MadProgrammer
7个回答

4

请尝试:

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. \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)

看看我在@Ronex答案上的评论。如果输出到“屏幕”,它只能在Windows上工作。如果你重定向到文件,很多Windows应用程序都无法处理它。 - Stephen C
我已更新答案,使其具有跨平台友好性。 - userlond

2
以下是翻译的结果:

以下是正在发生的事情。

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

您现在已经打印了一堆字符,全部在同一行上。由于使用了print并且没有显式地打印换行符,下一个要打印的字符也将放在同一行上。

System.out.println();

这将打印一个换行符 ('\n'),它与打印空白行是不同的。相反,它会导致下一个要打印的字符进入当前行以下的一行。

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

由于您刚刚打印了一个换行符,所以这段文本将会出现在“我有一个问题”的下一行。此外,由于您调用了println,如果您在此之后立即打印任何内容,它将会出现在新的一行而不是同一行。

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

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?");

由于您现在在两行之间打印了两个换行符,因此您将实现所需的间隔。


1
请注意,在输出字符串时添加裸的“\n”可能会使您的代码具有特定于平台的特性。对于控制台输出,这可能是可以接受的,但如果该文件被另一个(平台本地)应用程序读取,则可能会出现奇怪的错误。
以下是一些推荐的方法...应该适用于所有平台:
  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: println uses 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: %n means the platform default end-of-line sequence.

    Note: there is a convenience printf method in the PrintWriter interface 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.


0

如果你想在Java中留下单行空格,你可以使用

System.out.println("");

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

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

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


1
已经有人回答了类似的问题,请提供一些新的内容。感谢你的努力。 - Rade_303

0

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

\n可以将光标移至下一行。


不好的想法。这在Windows上无法正常工作...因为行尾序列是“\r\n”。提示:尝试将输出重定向到文件,然后使用记事本打开它。 - Stephen C

0
你可以使用以下代码。通过这种方法,您可以使用任意数量的行间距。只需增加或减少“\n”的数量即可。
System.out.print("Hello \n\n\n\n");
System.out.print("World");

-1

这个怎么样?

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" 应该是跨平台友好的。

"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接