Java 用户选择的选项不会触发if语句
所以我正在制作一个小的文本冒险游戏。我只有3个月左右的编码经验,但我决定用我所掌握的很少的编码知识来承担这个大项目。我删掉了代码的开头,用户在那里选择属性,因为这是不相关的 当提示用户在Java 用户选择的选项不会触发if语句,java,if-statement,switch-statement,equals,java.util.scanner,Java,If Statement,Switch Statement,Equals,Java.util.scanner,所以我正在制作一个小的文本冒险游戏。我只有3个月左右的编码经验,但我决定用我所掌握的很少的编码知识来承担这个大项目。我删掉了代码的开头,用户在那里选择属性,因为这是不相关的 当提示用户在“做事情”和“不做事情”之间做出选择时,我当前遇到的问题就会发生。每当代码到达此阶段,两个选项都不会触发out.println 此外,我意识到将整个游戏放入switch(YN)可能效率很低,但我不知道在不编写方法和类的情况下,还有什么其他方法可以做到这一点,我真的不懂,所以我将坚持使用main方法 import
“做事情”
和“不做事情”
之间做出选择时,我当前遇到的问题就会发生。每当代码到达此阶段,两个选项都不会触发out.println
此外,我意识到将整个游戏放入switch(YN)
可能效率很低,但我不知道在不编写方法和类的情况下,还有什么其他方法可以做到这一点,我真的不懂,所以我将坚持使用main
方法
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;
}
}
}
当你要读一行空白的时候。您应该使用
nextLine()
:
我完全忘记了下一行的存在。我必须使用它,因为
choice 1a=reader.next()
将只存储它找到的下一个字符串,对吗?是的,您可以在读取后通过打印choice1a
找到它。
choice1a = reader.nextLine();