java 转义序列无效(有效的是 \b \t \n \f \r \” \' \\ )”语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15230420/
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
Invalid escape sequence (valid ones are \b \t \n \f \r \” \' \\ )" syntax error
提问by A4L
print(" $$$$$$\ $$$$$\ $$$$$$\ $$\ $$\ "+newline+
"$$ __$$\ \__$$ | $$ __$$\ $$ | $$ |"+newline+
"$$ / $$ | $$ | $$ / $$ | $$\ $$ |"+newline+
"$$$$$$$$ | $$ | $$$$$$$$ | $$$$ /"+newline+
"$$ __$$ | $$\ $$ | $$ __$$ | $$ $$< "+newline+
"$$ | $$ | $$ | $$ | $$ | $$ | $$ /$$\"+newline+
"$$ | $$ | $$$$$$ | $$ | $$ | $$ / $$ |"+newline+
"\__| \__| \______/ \__| \__| \__| \__|"+newline);
Hi all ! I am just trying to add ascii art to my games gui, and I get this error? How do I resolve this?
大家好 !我只是想将 ascii 艺术添加到我的游戏 gui 中,但出现此错误?我该如何解决?
回答by rgettman
The backslash character \
is an escape characterin Java. The compiler thinks that you are attempting to escape the next character, and \_
is an invalid escape sequence. You need to escape the backslash itself. Replace every \
with \\
.
反斜杠字符\
是Java 中的转义字符。编译器认为您正在尝试转义下一个字符,并且\_
是无效的转义序列。您需要转义反斜杠本身。每次更换\
使用\\
。
回答by A4L
to print a \
you need to specify \\
in your string literals.
要打印\
您需要\\
在字符串文字中指定的内容。
you don't need to type them all manually. your IDE might have a cool feature witch lets you escape all chars that need to be escaped when pasting to a string literal.
你不需要手动输入它们。您的 IDE 可能有一个很酷的功能,可以让您在粘贴到字符串文字时转义所有需要转义的字符。
for eclipse it is under:
对于日食,它位于:
Window > preferences > java > Editor > typing
窗口 > 首选项 > java > 编辑器 > 打字
then check Escape text when pasting to a string literal
然后检查 Escape text when pasting to a string literal
回答by NPE
You need to double up every backslash, i.e. replace each \
with \\
.
您需要将每个反斜杠加倍,即将每个反斜杠替换\
为\\
.
The backslash character has special meaning inside Java string literals. It denotes the start of so-called escape sequences. For example, \n
stands for "new line".
反斜杠字符在 Java 字符串文字中具有特殊含义。它表示所谓的转义序列的开始。例如,\n
代表“新行”。
The escape sequence \\
stands for a single backslash character.
转义序列\\
代表单个反斜杠字符。
回答by Ahmed Aeon Axan
escape the backslashes, basically replace all your backslashes with \\
逃避反斜杠,基本上用你所有的反斜杠替换 \\
回答by cowls
All your backslashes need to be escaped with double backslashes: e.g.
你所有的反斜杠都需要用双反斜杠转义:例如
print(" $$$$$\");