我被告知要编写一个程序,它将读取一个三个数组的文件,每个数组的大小为5 x 6,包含许多0和非零数字。然后,我将创建一个未定义行数的数组,但是有3列指示非零数字所在的位置。第一列是行索引,第二列是列索引。第三列包含实际的非零数字本身。丰富与非丰富的矩阵
这是一个非常迂回的计划。但我相信我的主要问题是这样的 -
- 当两个非零数字在同一行中,我无法在两个数字的新矩阵中打印出相同的行索引。我尝试将row-index设置为a,然后将row-index设置为单独的行计数器,但它仍然混乱并逐行增加行数。目前,我的想法已经空白。
我的代码打印的唯一方法是如果我将皮肤设置为1而不是0.但是这会抛出整个程序;我已将其更改为1以更清楚地说明我的问题
也许这对于某个学校任务有点过分,我很抱歉。如果这篇文章以某种方式背叛了规则,我会立即删除它。
谢谢任何愿意饶恕这一眼的人。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Prog465h {
static Scanner inFile = null;
public static void main(String[]args) {
try {
// create scanner to read file
inFile = new Scanner(new File ("prog465h.dat"));
} catch (FileNotFoundException e) {
System.out.println("File not found!");
System.exit(0);
}
while(inFile.hasNext()) {
int rows = inFile.nextInt();
int columns = inFile.nextInt();
int rind = 1;
int cr = 0; // count rows
int cc = 0; // count zeroes
int[][] first = new int[rows][columns];
for (int a = 0; a < first.length; a++) {
// catch the next
for (int b = 0; b < first[a].length; b++) {
first[a][b] = inFile.nextInt();
}
}
for (int a = 0; a < first.length; a++) {
for (int b = 0; b < first[a].length; b++) {
System.out.print(first[a][b] + " ");
if (first[a][b] != 0) {
rind++;
}
}
System.out.println(" ");
}
System.out.println("COUNTS ARE BELOW:");
int[][] mod = new int [rind][3]; // new array based on non-zeroes
for (int a = 0; a < first.length; a++) {
cc = 0;
for (int b = 0; b < first[a].length; b++) {
if (first[a][b] == 0) { // if there is a 0 increase number of columns counted
cc++;
} else { // if not--
mod[cr][2] = first[a][b]; // then make this nonzero number the last column of x row of mod.
// x row depends on...?
// the number of counted rows?
mod[cr][0] = (a+1); // put the number of rows counted for this number
mod[cr][1] = (cc+1); // put the number of 0's (aka columns) counted for this number
cc = 0;
}
}
cr++;
}
for (int a = 0; a < mod.length; a++) {
for (int b = 0; b < mod[a].length; b++) {
System.out.print(mod[a][b] + " ");
}
System.out.println(" ");
}
System.out.println("\n **** ALL DONE **** \n");
}
}
}
我的输出:(。请注意如何第一个矩阵,它打印3 0的这不应该发生的事情,它应该只是跳过该行完全)
0 0 7 0 0 0
0 0 0 0 -8 0
0 0 0 0 0 0
2 0 0 0 0 0
0 0 0 0 0 0
COUNTS ARE BELOW:
1 3 7
2 5 -8
0 0 0
4 1 2
**** ALL DONE ****
0 2 0 3 0 1
8 0 4 0 1 0
0 3 0 1 0 -7
5 0 9 0 6 0
0 2 0 -1 0 7
COUNTS ARE BELOW:
1 2 1
2 2 1
3 2 -7
4 2 6
5 2 7
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
**** ALL DONE ****
0 0 1 0 0 2
3 0 0 4 0 0
0 0 5 0 0 6
7 0 0 8 0 0
0 0 9 0 0 1
COUNTS ARE BELOW:
1 3 2
2 3 4
3 3 6
4 3 8
5 3 1
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
**** ALL DONE ****
样本输出(什么输出应为):
Original Matrix
0 0 7 0 0 0
0 0 0 0 -8 0
0 0 0 0 0 0
2 0 0 0 0 0
0 0 0 0 0 0
1 3 7
2 5 -8
4 1 2
The Original Matrix is Sparse
Original Matrix
0 2 0 3 0 1
8 0 4 0 1 0
0 3 0 1 0 -7
5 0 9 0 6 0
0 2 0 -1 0 7
The Original Matrix is Abundant
Original Matrix
0 0 1 0 0 2
3 0 0 4 0 0
0 0 5 0 0 6
7 0 0 8 0 0
0 0 9 0 0 1
1 3 1
1 6 2
2 1 3
2 4 4
3 3 5
0 0 9
4 1 7
4 4 8
5 3 9
5 6 1
The Original Matrix and the Sparse Matrix
are Equally Efficient
文件:
5
6
0 0 7 0 0 0
0 0 0 0 -8 0
0 0 0 0 0 0
2 0 0 0 0 0
0 0 0 0 0 0
5
6
0 2 0 3 0 1
8 0 4 0 1 0
0 3 0 1 0 -7
5 0 9 0 6 0
0 2 0 -1 0 7
5
6
0 0 1 0 0 2
3 0 0 4 0 0
0 0 5 0 0 6
7 0 0 8 0 0
0 0 9 0 0 1
哇,非常感谢!它现在完美。我也会回去纠正我的for循环。感谢您的推荐,并给予我很好的建议。祝你今天愉快! – Nikolas
@尼古拉斯很高兴帮助。我确实注意到在最后一个矩阵中输出是错误的。您需要在内部循环中将'cc = 0;'更改为'cC++'。我编辑了我的答案。 –