Scanner を使用してオブジェクトを作成しようとしています。そのようなコンパイル後にオブジェクトを作成できますか? それとも事前に作ってもらっていますか?
質問をしてから、スキャナーを実行します。その後、スキャナーからの文字列を使用して、オブジェクトを作成しようとします。これは可能ですか?重要な場合とそうでない場合があるコードは次のとおりです。
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+ "."
...