可能涉及到的人,
我正在尝试使用单个打印行来编译 Java 程序。我的任务是使用转义序列,而我得到的错误是非法转义字符。这是我想要的输出。我想要的输出:
Alpha
Bravo
Charlie
Delta
EchoFoxtrotGolf
Hotel
India
This is a test.
这是我的代码:
public class PrintingChoicesEscapesSecond {
public static void main( String[] args ) {
System.out.println( "Alpha\n\Bravo\n\n\Charlie\n\Delta\n\n\EchoFoxtrotGolf\n\Hotel\n\n\India\n\n\This is a test. );
}
}
最佳答案
有两件事:
您缺少右引号
您在
\n
之后有\
,它将尝试转义下一个字母,这不是您想要的。
所以在字符串
中:
Alpha\n\Bravo
^^ this \ is trying to escape B which is invalid.
删除尾随的\
。另外,您还有一些额外的 \n
来获得您想要的输出。(Bravo 和 Hotel 之后的额外一个):
System.out.println("Alpha\nBravo\nCharlie\nDelta\n\nEchoFoxtrotGolf\nHotel\nIndia\n\nThis is a test. ");
输出:
Alpha
Bravo
Charlie
Delta
EchoFoxtrotGolf
Hotel
India
This is a test.
关于java - 新行转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53195808/