我的大脑可能已经停止工作,因此我真的需要您的帮助。
所以,我有一个输入txt文件,格式如下:
3 //Number of Heroes (nodes lets say)
43 43 // left int = hp, right int = damage etc...
22 444
12 43
2 //Number of enemies
20 39 //likewise with the heroes
//The latter (enemies) is not yet implemented, cause I am stuck with the
//first part (so let's say the part before '2' is present in the input
//txt at the moment
我的主要目的是(我希望将来能够处理多个文件):
public static void main(String[] args) {
Q2 ks = new Q2();
int length = args.length;
for (int i = 0; length > 0; i++) {
ks.readFile(args[i]);
break;
}
}
这是我的readFile(),我在其中尝试用数据填充矩阵(但是由于我的大脑实际上已经停止工作而失败,这很惨):
public int[][] readFile(String inputName) {
BufferedReader reader;
int Matrix[][] = null;
try {
reader = new BufferedReader(new FileReader(inputName));
String line = reader.readLine();
int numberOfHeroes = Integer.parseInt(line);
Matrix = new int[numberOfHeroes][2];
line = reader.readLine();
System.out.println(numberOfHeroes);
for (int i = 0; i < numberOfHeroes; i++) {
int hitpoints = 0;
int damage = 0;
//while (line != null) {
String splittedLine[] = line.split(" ");
//while (splittedLine[1] != null){
hitpoints = Integer.parseInt(splittedLine[0]);
damage = Integer.parseInt(splittedLine[1]);
Matrix[i][0] = hitpoints;
Matrix[i][i+1] = damage;
line = reader.readLine();
//}
//}
}
System.out.println("====GRAPH====");
for (int i = 0; i < numberOfHeroes; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(Matrix[i][j] + " ");
}
System.out.println();
}
System.out.println("============");
} catch(IOException e) {
return null;
}
return Matrix;
}
答案 0 :(得分:0)
您将damage
值放入错误的单元格中。您想要执行Matrix[i][i+1] = damage;
而不是Matrix[i][1] = damage;
。