Reputation: 143
I am starting to make a hangman game in java.
I want to be able to call my method states based on if the user gets a character wrong or not.
I am testing all of the states to make sure they display correctly.
State 1 and 2 display correctly, but not 3-5.
Here is the code:
public void state_1() {
System.out.println(" \n"
+ " ------\n"
+ "| |\n"
+ "| O\n"
+ "|\n"
+ "|\n"
+ "|\n"
+ "|\n"
+ "|\n"
+ "|\n"
+ "|");
System.out.println("\n"
+ "");
}
public void state_2() {
System.out.println(" \n"
+ " ------\n"
+ "| |\n"
+ "| O\n"
+ "| |\n"
+ "| |\n"
+ "| |\n"
+ "|\n"
+ "|\n"
+ "|\n"
+ "|");
System.out.println("\n"
+ "");
}
public void state_3() {
System.out.println(" \n"
+ " ------\n"
+ "| |\n"
+ "| O\n"
+ "| |\n"
+ "| |\n"
+ "| |\n"
+ "| |\n"
+ "| / \\n"
+ "| / \\n"
+ "| / \\");
System.out.println("\n"
+ "");
}
public void state_4() {
System.out.println(" \n"
+ " ------\n"
+ "| |\n"
+ "| O /\n"
+ "| | /\n"
+ "| |/\n"
+ "| |\n"
+ "| |\n"
+ "| / \\n"
+ "| / \\n"
+ "| / \\");
System.out.println("\n"
+ "");
}
public void state_5() {
System.out.println(" \n"
+ " ------\n"
+ "| |\n"
+ "| \\ O /\n"
+ "| \\ | /\n"
+ "| \\|/\n"
+ "| |\n"
+ "| |\n"
+ "| / \\n"
+ "| / \\n"
+ "| / \\");
System.out.println("\n"
+ "");
}
Here is the weird output:
------
| |
| O
|
|
|
|
|
|
|
------
| |
| O
| |
| |
| |
|
|
|
|
------
| |
| O
| |
| |
| |
| |
| / \n| / \n| / \
------
| |
| O /
| | /
| |/
| |
| |
| / \n| / \n| / \
------
| |
| \ O /
| \ | /
| \|/
| |
| |
| / \n| / \n| / \
I am sure there is something simple to be done, but I am just not for sure as I know its having a problem with the \ part before new line "\n" is created.
How to get around this is something I need your help with.
Upvotes: 1
Views: 2026
Reputation: 191914
"\\n"
makes a literal \n
in the printed output.
If you want a \
, and a newline, that is "\\\n"
, where you escape the slash, then put newline.
You could also just println()
each line individually. Still need to escape the backslashes, though
System.out.println();
System.out.println(" ------ ");
System.out.println("| |");
System.out.println("| O");
...
System.out.println("| / \\");
System.out.println("| / \\");
Upvotes: 4