Reputation: 303
So I'm in the process of making a little text adventure game. I have only 3 months or so of coding experience, but I've decided to take on this big project with the little coding knowledge I have. I cut out the beginning of the code where the user selects attributes because it's irrelevant.
The problem I'm currently running into happens when the user is prompted to make the choice between "do thing"
and "don't do thing"
. Whenever the code gets to this stage neither choice will trigger the out.println
.
Also, I realize putting the entire game into switch(YN)
is probably very inefficient, but I have no knowledge of any other way of doing this without writing methods and classes, and I really don't understand any of that, so I'm going to stick with this main
method.
import static java.lang.System.*;
import java.io.*;
import java.util.*;
public class TextAdventure{
public static void main(String args[]){
Scanner reader=new Scanner(in);
out.println("Do you wish to continue with these attributes? Y\\N? ");
String answer=reader.next();
answer=answer.toUpperCase();
char YN=answer.charAt(0);
String choice1="",choice1a="";
switch(YN){
case 'N':
out.println("\nThe great game god now smites you so you may select different attributes.\n");
break;
case 'Y':
out.println("\nThis is a placeholder for the game."+
"\ndo don't");
while(!(choice1.equals("do"))&&!(choice1.equals("don't"))){
choice1=reader.next();
choice1=choice1.toLowerCase();
}
if(choice1.equals("do")){
out.println("\nThis is the path for \"do\". What do you do now?"+
"\ndo thing don't do thing");
while(!(choice1a.equals("do thing"))&&!(choice1a.equals("don't do thing"))){
choice1a=reader.next();
choice1a=choice1a.toLowerCase();
}
if(choice1a.equals("do thing")){
out.println("\nYou did thing, and you have tripped and fallen. You are dead.");
break;
}
else if(choice1a.equals("don't do thing")){
out.println("\nYou didn't do thing. Good choice. But the game god smites you out of boredom.");
break;
}
}
else if(choice1.equals("don't"))
out.println("\nThis is the path for \"don't\". The great game god now smites you.\n");
break;
default:
out.println("\nYou must select an available character choice. The great game god now smites you.\n");
break;
}
}
}
Upvotes: 0
Views: 662
Reputation: 34166
When you are going to read a line with blanks. You should use nextLine()
:
choice1a = reader.nextLine();
Upvotes: 1