這是我在大學裏開展的一個項目,除了初始化遊戲的遊戲課程之外,一切似乎都很好。這裏是一個片段從csv文件中讀取
public class Game{
private Player player;
private World world;
private ArrayList<NonPlayableFighter> weakFoes;
private ArrayList<NonPlayableFighter> strongFoes;
private ArrayList<Attack> attacks;
private ArrayList<Dragon> dragons;
public Game() throws IOException{
player = new Player("");
world = new World();
weakFoes = new ArrayList<NonPlayableFighter>();
strongFoes = new ArrayList<NonPlayableFighter>();
attacks = new ArrayList<Attack>();
dragons = new ArrayList<Dragon>();
loadAttacks ("Database-Attacks_20309.csv");
loadFoes ("Database-Foes_20311.csv");
loadDragons ("Database-Dragons_20310.csv");
}
之後,後面跟着一些getters和我應該實現的4個方法。 這些方法loadCSV(String filePath)
,loadAttacks(String filePath)
,loadFoes(String filePath)
,loadDragons(String filePath)
我創建loadCSV(String filePath)
,使得它返回的String []這裏的ArrayList:
private ArrayList<String[]> loadCSV(String filePath) throws IOException{
String currentLine = "";
ArrayList<String[]> result = new ArrayList<String[]>();
FileReader fileReader = new FileReader(filePath);
BufferedReader br = new BufferedReader(fileReader);
currentLine = br.readLine();
while (currentLine != null){
String[] split = currentLine.split(",");
result.add(split);
}
br.close();
return result;
}
然後我想加載一些攻擊,敵人,和龍和將它們插入適當的ArrayList中。
我施加loadAttacks(String filePath)
這裏:
private void loadAttacks(String filePath) throws IOException{
ArrayList<String[]> allAttacks = loadCSV(filePath);
for(int i = 0; i < allAttacks.size(); i++){
String[] current = allAttacks.get(i);
Attack temp = null;
switch(current[0]){
case "SA": temp = new SuperAttack(current[1],
Integer.parseInt(current[2]));
break;
case "UA": temp = new UltimateAttack(current[1],
Integer.parseInt(current[2]));
break;
case "MC": temp = new MaximumCharge();
break;
case "SS": temp = new SuperSaiyan();
break;
}
attacks.add(temp);
}
}
我寫,使得它需要該ArrayList使用開關從而創建適當的攻擊並添加從loadCSV(String filePath)
和搜索返回在每個String []在第一字符串ArrayList的內它來攻擊。
然後我想爲Foes讀取另一個CSV文件,並且CSV文件的結構是這樣的,第一行有一些屬性,第二行有一些類型爲SuperAttack的攻擊,第三行有一些類型爲Ultimate攻擊的攻擊。同樣在每個敵人內部都有一個布爾屬性,用於判斷它是強還是弱敵,從而將它置於正確的Arraylist中。下面是loadFoes(String filePath)
代碼:
private void loadFoes(String filePath) throws IOException{
ArrayList<String[]> allFoes = loadCSV(filePath);
for(int i = 0; i < allFoes.size(); i += 3){
String[] current = allFoes.get(i);
String[] supers = allFoes.get(i+1);
String[] ultimates = allFoes.get(i+2);
ArrayList<SuperAttack> superAttacks = new ArrayList<SuperAttack>();
ArrayList<UltimateAttack> ultimateAttacks = new ArrayList<UltimateAttack>();
NonPlayableFighter temp = null;
for(int j = 0; i < supers.length; j++){
int index = attacks.indexOf(supers[j]);
if(index != -1){
superAttacks.add((SuperAttack)attacks.get(index));
}
else break;
}
for(int j = 0; i < ultimates.length; j++){
int index = attacks.indexOf(ultimates[j]);
if(index != -1){
ultimateAttacks.add((UltimateAttack)attacks.get(index));
}
else break;
}
if(current[7].equalsIgnoreCase("True")){
temp = new NonPlayableFighter(current[0], Integer.parseInt(current[1]),
Integer.parseInt(current[2]), Integer.parseInt(current[3]),
Integer.parseInt(current[4]), Integer.parseInt(current[5]),
Integer.parseInt(current[6]), true, superAttacks, ultimateAttacks);
strongFoes.add(temp);
}
else{
temp = new NonPlayableFighter(current[0], Integer.parseInt(current[1]),
Integer.parseInt(current[2]), Integer.parseInt(current[3]),
Integer.parseInt(current[4]), Integer.parseInt(current[5]),
Integer.parseInt(current[6]), false, superAttacks, ultimateAttacks);
weakFoes.add(temp);
}
}
}
首先我拿到前三的String [] ArrayList中從loadCSV(String filePath
返回並取得2個循環檢查,如果攻擊是以前加載攻擊中CSV然後我檢查了屬性,確定它是強還是弱,並相應地創建一個新的NonPlayableFighter
並將其添加到適當的列表中。
運行這個賦值的jUnit4測試,它給了我一個編譯錯誤:未處理的異常類型IOException。一般來說,代碼有什麼顯着的問題?
你是否在Main Class或main()中處理異常(使用try/catch或throws)? – Mahendra
進入loadCSV()方法,加入currentLine = br.readLine();進入while循環。 –