我是学习 Java 的新手。我大约一周前开始学习,每天花大约 10 个小时学习,我不确定我是否落后于我“应该”的地方(我正在处理的另一个问题)我知道我的代码可能不是最有效的,但我必须从某个地方开始,而且我已经从上周阅读的信息宇宙中输入了大部分内容。现在,我只是在尝试制作游戏或应用程序来帮助巩固编程概念。把我学到的东西用起来并使这些信息相关。
我的问题是,重要的是,为什么我的“if”语句直接指向“else”?
public class ClassSelect {
public static String className;
public static void pickPlayer() {
Scanner scan = new Scanner(System.in);
System.out.println("Pick your class." + "\n" + "[F]ighter" + "\n"
+ "[W]izard");
String scanClass = scan.nextLine();
if ((scanClass == "f") || (scanClass == "F")) {
System.out.println("You picked the Fighter");
String classNameF = "Fighter";
className = classNameF;
try {
File file = new File("saveData.txt");
BufferedWriter output = new BufferedWriter(new FileWriter(file));
output.write(className);
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
else if ((scanClass == "w") || (scanClass == "W")) {
System.out.println("You picked the Wizard");
String classNameW = "Wizard";
className = classNameW;
try {
File file = new File("saveData.txt");
BufferedWriter output = new BufferedWriter(new FileWriter(file));
output.write(className);
output.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("You didn't choose either.");
}
}