我被告知要编写一个程序,该程序将读取三个数组的文件,每个数组的大小为5 x 6,包含许多0和非零数字。然后我将创建一个具有未定义行数但3列的数组,这些列指示非零数字的位置。第一列是行索引,第二列是列索引。第三列包含实际的非零数字。
这是一个非常迂回的计划。但我相信我的主要问题是 -
我的代码打印的唯一方法是,如果我将我的外皮设置为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");
}
}
}
我的输出:(注意如何为第一个矩阵打印三个0&#39; s。这不应该发生,它应该完全跳过该行。)
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
答案 0 :(得分:1)
您对此作业的前瞻性表示赞赏。一般情况下,如果您发布此类问题,我认为您不会遇到问题。你已经明确地尝试了这个问题,并且非常接近它的工作原理。
我看了一下,看起来你只需要稍微移动cr++;
一行。将它向上移动几行,使其位于内部for循环内(因此它会在行cc = 0;
之后立即执行。另外,请确保初始化int rind = 0;
(而不是1)。您将还需要将cc = 0;
更改为cc++;
。
当我运行它时,它会生成您在问题中发布的样本输出。
只是观察,但你也可以通过将2 for循环压缩成一个来整理你的代码:
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(" ");
}
可能成为:
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();
System.out.print(first[a][b] + " ");
if (first[a][b] != 0) {
rind++;
}
}
System.out.println(" ");
}