我刚刚开始学习Java,并认为我将开始一个小项目以了解更多基本概念。当我输入诸如“ Warrior”之类的输入时,我就可以使用它,并且可以输出您选择的战士。但是,我在“ public static void Goldshire()”中添加了第二个扫描器。然后由于某种原因它停止工作。我不太确定自己做错了什么,花了大约一个小时的时间看了一下代码,却不明白自己做了什么。如果有人能够给我一些反馈,我将不胜感激。
import java.util.Scanner;
public class Game0 {
public static void main (String[] args) {
characterCreation();
}
//Character Creation
public static void characterCreation(){
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
String playerName;
byte currentHealth = 10;
String className;
System.out.println("Welcome to a text-based adventure game based on Zork! \n\nYou will now proceed to the naming of your character.");
System.out.println("\nEnter your characters name: ");
playerName = input.nextLine();
System.out.println("Your adventure will now begin "+playerName + ".");
System.out.println("\nYour current health is: " + currentHealth);
System.out.println("\nYou will now choose your class:\n \n 1. Warrior\n 2. Mage\n 3. Rouge");
className = input.nextLine();
if(className == "Warrior") {
System.out.println("You have chosen Warrior.");
}
if (className =="Mage") {
System.out.println("You have chosen Mage.");
}
if (className =="Rouge") {
System.out.println("You have chosen Rouge.");
}
else {
System.out.println("You have chosen an invalid class! The game will now end because I cannot get a loop working (:");
//System.exit(10);
}
}
//First Zone
public static void Goldshire() {
@SuppressWarnings("resource")
Scanner scan2 = new Scanner(System.in);
characterCreation();
byte decision1;
System.out.println("\nYou are currently at an inn in Goldshire. You can either: \n\n1. Talk to the innkeeper. \n2. Head out of the inn.");
decision1 = scan2.nextByte();
if(decision1==1) {
System.out.println("You speak to the innkeeper, he tells you that a Knight outside the inn wants to speak with you. You head outside");
}
if(decision1 ==2) {
System.out.println("You head outside, a Knight urgently calls you over.");
}
System.out.println("The Knight has a few words.");
}
}