java在文件中逐字读取
我目前正在为一个班级项目制作一个原始蚂蚁vs僵尸游戏。我们将读入一个“部落”文件,该文件包含将要入侵的僵尸对应的字符和一个整数(包括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
我想对每个数字进行硬编码,但还是遇到了不知道如何将同一个对象的倍数相加的问题。我真的很感激任何帮助。提前谢谢大家
共 (0) 个答案