Invalid escape sequence (valid ones are \\b \\t \\

2019-01-27 05:56发布

问题:

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?

回答1:

The backslash character \ is an escape character in 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 \\.



回答2:

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.

for eclipse it is under:

Window > preferences > java > Editor > typing

then check Escape text when pasting to a string literal



回答3:

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".

The escape sequence \\ stands for a single backslash character.



回答4:

escape the backslashes, basically replace all your backslashes with \\



回答5:

All your backslashes need to be escaped with double backslashes: e.g.

print(" $$$$$\\");