如何从 args 中获取的输入文本填充二维数组(矩阵)?
How to populate 2D array (Matrix) from input txt taken from args?
我的大脑可能已经停止工作了,所以我真的需要你的帮助。
所以,我有一个格式如下的输入 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;
}
您将 damage
值放入错误的单元格中。而不是 Matrix[i][i+1] = damage;
你想做 Matrix[i][1] = damage;
.
我的大脑可能已经停止工作了,所以我真的需要你的帮助。
所以,我有一个格式如下的输入 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;
}
您将 damage
值放入错误的单元格中。而不是 Matrix[i][i+1] = damage;
你想做 Matrix[i][1] = damage;
.