我目前正在为一个类项目制作一个原始蚂蚁vs僵尸游戏。我们要读入一个'horde'文件,其中包含对应于将要入侵的僵尸的字符以及表示前一个僵尸字符的倍数的整数(1-9包含)。我遇到的麻烦是区分文件字符串中的int和char,以及如何根据int创建多个对象。这是我到目前为止有:在文件中逐字符地读
public void readHordeFile(String filename){
try {
file = new FileReader(filename);
} catch (FileNotFoundException e) {
System.out.println("File not found " + e.getMessage());
}
buf = new BufferedReader(file);
try {
zombieString = buf.readLine();
for(int i = 0; i < zombieString.length(); i++){
if(zombieString.charAt(i) == 'S'){
horde.add(new ZombieScientist());
}else if(zombieString.charAt(i) == 'Z'){
horde.add(new StandardZombie());
}else if(zombieString.charAt(i) == 'I'){
horde.add(new InfectedZombie());
}else if(zombieString.charAt(i) == 1){
}
}
} catch (IOException e) {
e.getMessage();
}
}
一个例子文件包含:SZI1
我想的只是硬编码每一个数字,但我仍然会碰到不知道怎么加的倍数的问题同一个对象。我真的很感激任何帮助。谢谢大家。
我不明白这个问题 –
在文件中的例子是否意味着有1个S和Z和I? – mcalex
我的问题是如何区分字符串中的字符是int还是如何根据int创建对象的倍数编辑:如果文件是SZ2I1,我无法控制文件内容 – somtingwong