我不想添加所有代码,因为这是我目前正在制作的游戏。无论如何,我声明了 class_Selection 和 Characterclass。用户为 class_Selection 输入一个字符串。它显示结果,然后进入一个 if 语句,在该语句中它使用相同的输入来决定之后要做什么。我对java非常陌生,并且为此编码,这是我的第一个项目。问题可能很简单。如您所见,我将其全部大写,因为我听说如果它全部大写,无论用户如何输入,它都会接受该密钥,例如:MeLeE,那可能不是真的。结果在正确通过程序的其余部分时产生,甚至显示了 class_Selection 的结果,但是当它到达 if 语句时,它显示为 null。
编辑 问题是某种字符类等于 null 所以当我 System.out.println(Characterclass); 我收到空值。
我修复了 ==,谢谢你,但我需要修复 CharacterClass 以某种方式最终为空。
编辑问题是,在 IF 语句中,我得到了全部大写的答案,我知道可能有办法做到这一点,但我做得不对。如果我输入正确的键,例如如果它是 MELEE,我输入 MELEE 它将显示结果。我也想当我做 system.out.println(Characterclass); ,我没有运行它的方法,所以我要修复它。我得到它来显示奖金。
谢谢你的==问题,我离我的目标更远了。
// 类选择
System.out.println("Which one did you excel in ( Class )? Melee, Magic, or Archery?"); // Class selection
String class_Selection; // Hold the class name (make method with this to declare bonuses and things that have to do with the class he chose: such as If ( class_Selection == melee)
class_Selection = classSelection.next(); // User enters name of class they want
System.out.println("You entered: " + class_Selection + (" Is this the class you want?, y or n?"));
String yOrN;
yOrN = defaultScanner.next();
if (yOrN == "n") { // redo if selection is no
System.out.println("Then which class would you like? Melee, Magic, Archery?");
class_Selection = classSelection.next();
System.out.println("You entered: " + class_Selection + (" Is this the class you want?, y or n?"));
yOrN = defaultScanner.next();
}else{ // Does nothing if y because the selection is correct
}
// Continue after class selection
System.out.println("Your class is: " + class_Selection); // Final display
System.out.println("\n");
// This is where your selection takes effect and displays your bonuses
if (class_Selection == "MELEE") {
Characterclass = "melee";
System.out.println("Melee = +5 Blade, +5 Blunt, +5 Hand-To-Hand, +5 Heavy Armor,");
}
if (class_Selection == "ARCHERY") {
Characterclass = "Archery";
System.out.println("+10 Archery, + 10 Light Armor");
}
if (class_Selection == "MAGIC") {
Characterclass = "Magic";
System.out.println(" +10 Arcane, +5 Light Armor");
}
System.out.println(Characterclass);
}
}