当我写出我的字符串时,它告诉我字符串文字没有正确关闭,但看起来它是......?然后,如果我取出/ | \,错误向下移动2行到腿.我研究过,似乎无法知道问题是什么......
public static void printMan(int badGuesses) {
String[] man = new String[];
man={"______",
"| |",
"| o",
"| |",
"| /|\", //it tells me that i need to insert missing quote
"| |",
"| / \"
};
int counter = 0;
while (counter < badGuesses) {
System.out.println(man[counter]);
}
Run Code Online (Sandbox Code Playgroud)
\是一个转义字符,在这种情况下你也需要逃避它.否则,你会得到一个未终止的字符串.\"表示实际字符,"而不是字符串的开头或结尾.
如果你想要实际的角色\你也需要逃脱它:\\
String[] man = new String[]{
"| |",
"| o",
"| |",
"| /|\\", \\<- note the extra \
"| |",
"| / \\" \\<- note the extra \ here too
};
Run Code Online (Sandbox Code Playgroud)
以反斜杠(\)开头的字符是转义序列,对编译器具有特殊含义.下表显示了Java转义序列(me:链接中的表)
这当然符合语言规范:
StringLiteral:
"StringCharacters"
StringCharacters:
StringCharacter
| StringCharacters StringCharacter
StringCharacter:
InputCharacter but not " or \
| EscapeSequence
EscapeSequence:
\ b
\ t
\ n
\ f
\ r
\ "
\ '
\ \ < - **THIS IS THE ONE YOU HAD**
OctalEscape /* \u0000 to \u00ff: from octal value */
Run Code Online (Sandbox Code Playgroud)
(相关问题)
在计算和通信中,转义字符是一个字符,它对字符序列中的后续字符调用另一种解释.转义字符是元字符的特殊情况.通常,判断某事物是否属于逃避角色取决于背景.
注意到:
C,C++,Java和Ruby都允许完全相同的两个反斜杠转义样式.
这里是关于逃避字符串的另一个相关问题.
| 归档时间: |
|
| 查看次数: |
1405 次 |
| 最近记录: |