我用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”做什么?我尝试在谷歌上搜索,但由于它是如此少量的代码,我找不到多少。
答案 0 :(得分:36)
这是一个新的行
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
答案 1 :(得分:6)
\n
这意味着会打印一个新行。
作为附注,没有必要写出额外的一行。那里有一个内置的内置功能。</ p>
println() //prints the content in new line
答案 2 :(得分:6)
\n
这意味着此时在文本中插入换行符。
只是示例
System.out.println("hello\nworld");
输出:
hello
world
答案 3 :(得分:2)
(根据http://java.sun.com/...ex/Pattern.html)
反斜杠字符('\
')用于引入如上表中定义的转义构造,以及引用否则将被解释为非转义构造的字符。因此,表达式\\
匹配单个反斜杠,{匹配左括号。
其他用法示例:
\\ The backslash character<br>
\t The tab character ('\u0009')<br>
\n The newline (line feed) character ('\u000A')<br>
\r The carriage-return character ('\u000D')<br>
\f The form-feed character ('\u000C')<br>
\a The alert (bell) character ('\u0007')<br>
\e The escape character ('\u001B')<br>
\cx The control character corresponding to x <br>
答案 4 :(得分:1)
\n
是用新行对象替换的字符串的转义字符。在打印输出的字符串中写\n
将打印出新行,而不是\n
答案 5 :(得分:0)
在原始问题的代码示例的特定情况下,
System.out.print("\n");
是否会在递增i之间移动到新的一行。
因此第一个print语句打印Grid [0] [j]的所有元素。当最里面的for循环完成时,打印“\ n”,然后在下一行打印Grid [1] [j]的所有元素,并重复这一过程,直到你有一个10x10的网格元素为止。二维数组,Grid。
答案 6 :(得分:0)
\ n添加一个新行。
请注意java有方法 System.out.println(“在这里写文字”);
注意区别:
代码:
System.out.println("Text 1");
System.out.println("Text 2");
输出:
Text 1
Text 2
代码:
System.out.print("Text 1");
System.out.print("Text 2");
输出:
Text 1Text 2