Java - “\\n”是什么意思?

新手上路,请多包涵

我在 Java 中创建了一个二维数组,我正在寻找一种方法将其打印在控制台上,以便我可以确认我正在制作的东西是正确的。我在网上找到了一些为我执行此任务的代码,但我对代码的特定部分的含义有疑问。

 int n = 10;
int[][] Grid = new int[n][n];

//some code dealing with populating Grid

void PrintGrid() {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            System.out.print(Grid[i][j] + " ");
        }
        System.out.print("\n");
    }
}

“\n”是做什么的?我试着在谷歌上搜索,但因为它是这么小的代码,所以我找不到太多。

原文由 dock_side_tough 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.2k
1 个回答

这是一条新线

Escape Sequences
Escape Sequence Description
\t  Insert a tab in the text at this point.
\b  Insert a backspace in the text at this point.
\n  Insert a newline in the text at this point.
\r  Insert a carriage return in the text at this point.
\f  Insert a formfeed in the text at this point.
\'  Insert a single quote character in the text at this point.
\"  Insert a double quote character in the text at this point.
\\  Insert a backslash character in the text at this point.

http://docs.oracle.com/javase/tutorial/java/data/characters.html

来自 Java 语言规范。

 EscapeSequence:
\ b (backspace BS, Unicode \u0008)
\ t (horizontal tab HT, Unicode \u0009)
\ n (linefeed LF, Unicode \u000a)
\ f (form feed FF, Unicode \u000c)
\ r (carriage return CR, Unicode \u000d)
\ " (double quote ", Unicode \u0022)
\ ' (single quote ', Unicode \u0027)
\ \ (backslash \, Unicode \u005c)

https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.6

原文由 Peter Lawrey 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏