在这个程序中,我尝试使用 java 打印编写器写入一个名为 m3 的输出文件。当 {{2,2,2},{2,2,2}} 的矩阵 f3 应生成输出文件时,以下输出的前两行保留用于行数和列数,如下所示f3 下的数组,输出文件应为 {{2},{3},{2,2,2},{2,2,2}}。我将问题隔离到主 try-catch 块中的 4 行代码。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Test {
/**
* Error to output if can't open any files
*/
public static final String ERR_FILE = "Error opening file(s)";
/**
* Error to output if files open but matrices
* are of incompatible dimensions for multiplication
*/
public static final String ERR_DIMS = "Bad matrix dimensions";
private static void _outputMatrix(PrintWriter out, int[][] m, boolean includeDimensions) {
for (int r=0; r<m.length; r++) {
if (includeDimensions && r==0) {
out.printf("%d%n%d%n", m.length, m[0].length);
}
for (int c=0; c<m[r].length; c++) {
out.printf("%d", m[r][c]);
out.printf((c<m[r].length-1)?" ":"%n");
}
}
}
/**
* Prints a matrix to the terminal
* without dimensions
*
* @param m matrix to print
*/
public static void printMatrix(int[][] m) {
_outputMatrix(new PrintWriter(System.out, true), m, false);
}
/**
* Prints a matrix to a file
* with associated dimensions
*
* @param m matrix to print
* @param pw open file
*/
public static void printMatrix(int[][] m, PrintWriter pw) {
_outputMatrix(pw, m, true);
}
/**
* Checks if two matrices can be multiplied
* (i.e. the columns of the first match
* the rows of the second)
*
* @param m1 matrix 1
* @param m2 matrix 2
* @return true if m1 x m2 is legal
*/
public static boolean canMultiply(int[][] m1, int[][] m2) {
int rows1 = m1.length;
int rows2 = m2.length;
if (rows1 == 0 || rows2 == 0) {
return false;
}
int columns1 = m1[0].length;
if (columns1 == rows2) {
return true;
}
return false;
}
/**
* Reads and returns a matrix from a scanner
* Format:
* m (# rows)
* n (# #cols)
* r0c0 r0c1 ... r0cn (values in row 0, column-by-column)
* r1c0 r1c1 ... r1cn (values in row 1, column-by-column)
* ...
* rmc0 rmc1 ... rmcn (values in last row, column-by-column)
*
* Results in...
* int[][] {
* {r0c0, r0c1, ... r0cn},
* {r1c0, r1c1, ... r1cn},
* ...
* {rmc0, rmc1, ... rmcn}
* }
*
* @param s input source
* @return resulting matrix
*/
public static int[][] readMatrix(Scanner s) {
int m = s.nextInt();
int n = s.nextInt();
int[][] matrix = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = s.nextInt();
}
}
return matrix;
}
/**
* Multiply two matrices and
* return the result (assumes
* input matrices are valid
* for multiplication)
*
* @param m1 matrix 1
* @param m2 matrix 2
* @return result of m1 x m2
*/
public static int[][] matrixMultiply(int[][] m1, int[][] m2) {
int rows1 = m1.length;
int columns1 = m1[0].length;
int columns2 = m2[0].length;
int[][] resultMatrix = new int[rows1][columns2];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < columns2; j++) {
for (int k = 0; k < columns1; k++) {
resultMatrix[i][j] += (m1[i][k] * m2[k][j]);
}
}
}
return resultMatrix;
}
/**
* Program to multiply matrices:
* 1. Ask for paths for 3 files (2 input, 1 output)
* 2. Check if inputs can be multiplied
* 3. If so, multiply!
* - Output the full problem to the console
* - Output only the result matrix to the file
*
* @param args command-line arguments, ignored
*/
public static void main(String[] args) {
// try(Scanner input = new Scanner(System.in)){
// String m1, m2, m3;
//
// System.out.print("Enter path for matrix 1: ");
// m1 = input.nextLine();
// System.out.print("Enter path for matrix 2: ");
// m2 = input.nextLine();
// System.out.print("Enter path for result: ");
// m3 = input.nextLine();
// int[][] f1, f2, f3;
//
// Scanner file1 = new Scanner(new File(m1));
// Scanner file2 = new Scanner(new File(m2));
// PrintWriter output = new PrintWriter(new File(m3));
//
//
// f1 = readMatrix(file1);
// f2 = readMatrix(file2);
//
//
//
//
// printMatrix(f1);
// System.out.println("X");
// printMatrix(f2);
// System.out.println("=");
//
// if (canMultiply(f1, f2) != true) {
// throw new Exception(ERR_DIMS);
// }
//
// f3 = matrixMultiply(f1, f2);
//
// printMatrix(f3);
//
// printMatrix(f3, output);
//
try {
int[][] f1 = {{2,2,2},{2,2,2}};
PrintWriter output = new PrintWriter(new File("m3.txt"));
printMatrix(f1);
printMatrix(f1, output);
} catch (FileNotFoundException e) {
System.out.println(ERR_FILE);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
我没有收到错误,而是没有任何内容被打印到 m3.txt 文件中。运行文件时不会抛出找不到文件,所以我的第一直觉是我初始化打印文件的方式有问题?