如何从java中的文字文本文档中获取2个矩阵?
我正在尝试制作这个程序,在这里我创建一个矩阵,并进行矩阵乘法。 我需要从如下文档中检索矩阵(是的,每个部分是分开的): 新矩阵的开始将用“矩阵”表示。新行的开头将用“行”表示,后跟分配给该行每列的编号。 完成的2个矩阵应如下所示:如何从java中的文字文本文档中获取2个矩阵?,java,matrix,file-io,matrix-multiplication,Java,Matrix,File Io,Matrix Multiplication,我正在尝试制作这个程序,在这里我创建一个矩阵,并进行矩阵乘法。 我需要从如下文档中检索矩阵(是的,每个部分是分开的): 新矩阵的开始将用“矩阵”表示。新行的开头将用“行”表示,后跟分配给该行每列的编号。 完成的2个矩阵应如下所示: { {1,2,-2,0}, {-3,4,7,2}, {6,0,3,1} }; { {-1, 3}, {0,9},{1,-11},{4,-5}}; 由于我需要从文件中分离每一行和每一列,我应该如何创建这两个矩阵?我不知道如何从文本中提取矩阵,因为我不知道如何将其与文本
{ {1,2,-2,0}, {-3,4,7,2}, {6,0,3,1} };
{ {-1, 3}, {0,9},{1,-11},{4,-5}};
由于我需要从文件中分离每一行和每一列,我应该如何创建这两个矩阵?我不知道如何从文本中提取矩阵,因为我不知道如何将其与文本分开。
这就是我在创建矩阵后所做的
public class Tester{
public static void main(String args[]){
int a[][] = new int[][]{ {1,2,-2,0}, {-3,4,7,2}, {6,0,3,1} };
int b[][] = new int[][]{ {-1, 3}, {0,9},{1,-11},{4,-5}};
int c[][] = MatrixMult.mult(a,b);
for (int[] row : c) {
for (int x : row) {
System.out.print(x + " ");
}
System.out.println("");
}
}
mult函数需要2个数组并返回矩阵乘法的乘积:
public class MatrixMult{
public static int[][] mult(int arrayA[][], int arrayB[][]){
int rowA = arrayA.length;
int colA = arrayA[0].length;
int rowB = arrayB.length;
int colB = arrayB[0].length;
int[][] product = new int[rowA][colB];
for(int x = 0; x < rowA; x++) {
for (int y = 0; y < colB; y++) {
for (int z = 0; z < colA; z++) {
product[x][y] += arrayA[x][z] * arrayB[z][y];
}
}
}
return product;
}
}
公共类矩阵结果{
公共静态int[]]mult(int-arrayA[]],int-arrayB[]]{
int rowA=arrayA.length;
int-colA=arrayA[0]。长度;
int rowB=arrayB.length;
int colB=arrayB[0]。长度;
int[][]产品=新int[rowA][colB];
对于(int x=0;x
这就是我尝试过的:
public class Tester{
public static void main(String args[]){
Scanner fileInput = new Scanner(new File("C:\\Users\\b8897\\Desktop\\DataGym.in"));
int maxIndex = -1;
String[] line = new String[1000];
int a[][] = new int[][];
int b[][] = new int[][];
while (fileInput.hasNext()){
Scanner sc = new Scanner()
}
fileInput.close();
for (int x = 0; x<=maxIndex;x++){
Scanner sc = new Scanner(line[x]);
while(sc.hasNext()){
if(sc.next().equals("matrix"){
total+= Double.parseDouble(sc.next());
}
}
公共类测试器{
公共静态void main(字符串参数[]){
扫描仪文件输入=新扫描仪(新文件(“C:\\Users\\b8897\\Desktop\\DataGym.in”);
int maxIndex=-1;
字符串[]行=新字符串[1000];
int a[][]=新int[]];
int b[][]=新int[]];
while(fileInput.hasNext()){
扫描仪sc=新扫描仪()
}
fileInput.close();
对于(int x=0;x
public class Tester{
public static void main(String args[]){
Scanner fileInput = new Scanner(new File("C:\\Users\\b8897\\Desktop\\DataGym.in"));
int maxIndex = -1;
String[] line = new String[1000];
int a[][] = new int[][];
int b[][] = new int[][];
while (fileInput.hasNext()){
Scanner sc = new Scanner()
}
fileInput.close();
for (int x = 0; x<=maxIndex;x++){
Scanner sc = new Scanner(line[x]);
while(sc.hasNext()){
if(sc.next().equals("matrix"){
total+= Double.parseDouble(sc.next());
}
}