我正在尝试使用扫描仪来创建对象。我只是想,我可以在编译之后创建一个对象吗?或者我事先做过吗?
所以我问一个问题,然后运行扫描仪。之后,我尝试使用扫描仪中的String创建一个对象。这可能吗?以下是可能或不重要的代码:
import java.util.Scanner;
public class CreateCharacter{
public static Scanner input = new Scanner( System.in );
public static String race;
public static String name;
public static String askRace(){
System.out.println();
System.out.println("What race do you want to be?");
System.out.print("(");
Character.getAllRaces();
System.out.println(")");
race = input.next();
System.out.println();
return race;
}
public static String askName(){
System.out.println();
System.out.println("What is your Name?");
name = input.next();
System.out.println();
return name;
}
public static void askCharDeets(){
askName();
askRace();
input.close();
}
public static void main(String[] arguments){
askCharDeets();
Character c1 = new Character(name, race);
//c1.Stats();
}
}
和
import java.util.HashMap;
public class Character extends Creature{
public static String[] races = {"Human", "Elf", "Dwarf", "Gnome"};
int hp;
int mp;
int spd;
int ac;
public Character(String charName, String race){
name = charName;
info = new HashMap<String, String>();
stats = new HashMap<String, Integer>();
info.put("Name", name);
info.put("Gender", "Male");
stats.put("Level", 1);
for(int i = 0; i < races.length; i++){
if(race == races[0]){
info.put("Race", races[0]);
stats.put("Fortitude", 9);
stats.put("Willpower", 9);
stats.put("Dexterity", 9);
stats.put("Strength", 9);
otherStats();
break;
}else if(race == races[1]){
info.put("Race", races[1]);
stats.put("Fortitude", 7);
stats.put("Willpower", 11);
stats.put("Dexterity",9);
stats.put("Strength", 9);
otherStats();
break;
}else if(race == races[2]){
info.put("Race", races[2]);
stats.put("Fortitude", 11);
stats.put("Willpower", 7);
stats.put("Dexterity", 9);
stats.put("Strength", 9);
otherStats();
break;
}else if(race == races[3]){
info.put("Race", races[3]);
stats.put("Fortitude", 9);
stats.put("Willpower", 9);
stats.put("Dexterity", 11);
stats.put("Strength", 7);
otherStats();
break;
}else{
System.out.println(race + " is not a valid Race Type for " + name+ ".");
break;
}
}
}
void otherStats(){
hp = stats.get("Fortitude") + stats.get("Level");
mp = stats.get("Willpower") + stats.get("Level");
spd = stats.get("Dexterity") + stats.get("Level");
ac = stats.get("Fortitude")/2 + stats.get("Level");
stats.put("Mana Points", 9);
stats.put("Hit Points", hp);
stats.put("Speed", 11);
stats.put("Armor Class", 7);
}
public void Stats(){
System.out.println("-"+name+"-");
System.out.println("Race " + info.get("Race"));
System.out.println("Class " + info.get("Class"));
System.out.println("Level " + stats.get("Level"));
System.out.println("---");
System.out.println("Hit Points " + stats.get("Hit Points"));
System.out.println("Mana Points " + stats.get("Mana Points"));
System.out.println("Speed " + stats.get("Speed"));
System.out.println("Armor Class " + stats.get("Armor Class"));
System.out.println("---");
System.out.println("Fortitude " + stats.get("Fortitude"));
System.out.println("Willpower " + stats.get("Willpower"));
System.out.println("Dexterity " + stats.get("Dexterity"));
System.out.println("Strength " + stats.get("Strength"));
}
public static void getAllRaces(){
for(int i = 0; i < races.length; i++){
if(i < races.length - 1){
System.out.print(races[i]+ ", ");
}else if(i == races.length - 1){
System.out.print(races[i]);
}
}
}
}
奇怪的是,它不是java错误,而是打印出race + " is not a valid Race Type for " + name+ "."
...