如何从文本文件创建矩阵 3x3



我必须从 txt 文件创建一个 3x3 矩阵,然后获取行列式。 我试图用这些数字来做到这一点:

22 10 15
12 5 8
22 3 8

但是当我运行程序时,矩阵是:

22 10 15 
12 5 88 
22 3 8

嗯.. 不确定你到底期望什么

int [] matrix = new int [9];
File file = newFile("filepath")
try (BufferedReader br = new BufferedReader(new FileReader(file))) 
{
String line;
int i=0;
while ((line = br.readLine()) != null) 
{
String [] numbers=line.split(" ");
for(int j=0;j<numbers.length;j++)
{
if(numbers[j].matches(("-?\d+")))
{
matrix[i] = Integer.parseInt(numbers[j]);
i++;
}
}
}
System.out.println("determinant: "+ Integer.toString(matrix[0]*matrix[4]*matrix[8] + matrix[1]*matrix[5]*matrix[6] + matrix[2]*matrix[3]*matrix[7]));
} catch (IOException e) 
{
e.printStackTrace();
}

这是一个如何计算行列式的示例

最新更新