[英]Reading two matrices from the same file in java
大家好,提前感謝您的幫助!
我正在嘗試用 java 做一個矩陣計算器
從同一個文件中讀取兩個矩陣,如下所示:
2 2
34 78
89 -12
@
2 2
67 76
123 5
第一行是排名
第二行和第三行是第一個矩陣
“@”分割第一個和第二個矩陣
這就是我想出的代碼,但我沒有
找到與此問題類似的任何東西...有人可以幫助我嗎?
String [] line = new String[30];
int counter = 2;
int rank[] = new int[2];
int matrixa[][] = new int [3][3];
try {
BufferedReader MyReader = new BufferedReader(new FileReader("matrix.txt"));
while(line != null) {
line = MyReader.readLine().split(" ");
}
rank[0] = Integer.parseInt(line[0]);
rank[1] = Integer.parseInt(line[1]);
for(int i = 0; i <rank[0];i++) {
for (int j=0;j<rank[1];j++) {
matrixa[i][j] = Integer.parseInt(line[counter]);
counter++;
System.out.print(matrixa[i][j] + " ");
}
System.out.println();
} }catch (Exception e) {}
當您的行等於“@”時留下一個行情變量
int ticker;
for(int i = 0; i < lines.length; i++){
if(lines[i].equals("@")) ticker = i;
}
然后可以使用此代碼來打破數組。 如果需要,這也可以更早地用於將數組分解為 2 個單獨的數組。
String currentLine;
String[] line = new String[15];
String[] line2 = new String[15];
int i = 0;
while(line != null && !currentLine.equals("@")){
line = MyReader.readLine().split("");
currentLine = line[i];
if(currentLine.equals("@")) line[i] = null;
i++;
}
while(line2 != null)){
line2 = MyReader.readLine().split("");
}
它們可以被解析成矩陣,然后可以從那里對它們進行數學運算。 祝你的努力好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.