import java.util.Scanner;
public class BooleanProduct {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = 10;
int[][] A = new int[n][n];
int[][] B = new int[n][n];
// Fill A and B
int m, n2, p, q;
System.out.println("Enter number of rows for matrix A : ");
m = input.nextInt();
System.out.println("Enter number of columns for matrix A : ");
n2 = input.nextInt();
System.out.println("Enter number of rows for matrix B : ");
p = input.nextInt();
System.out.println("Enter number of columns for matrix B : ");
q = input.nextInt();
System.out.println("Enter elements for matrix A (by rows): ");
for (int i = 0; i < m; i++)
for (int j = 0; j < n2; j++) {
A[i][j] = input.nextInt();
}
System.out.println("Enter elements for matrix B : ");
for (int i = 0; i < p; i++)
for (int j = 0; j < q; j++) {
B[i][j] = input.nextInt();
}
System.out.println("Matrix A: ");
for (int i = 0; i < m; i++) {
System.out.println();
for (int j = 0; j < n2; j++) {
System.out.print(A[i][j] + " ");
}
}
System.out.println();
System.out.println("Matrix B: ");
for (int i = 0; i < p; i++) {
System.out.println();
for (int j = 0; j < q; j++) {
System.out.print(B[i][j] + " ");
}
}
System.out.println();
}
int[][] C = booleanProduct(A, B, n);
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(C[i][j] + " ");
}
System.out.println("");
}
}
static int[][] booleanProduct(int[][] A, int[][] B, int n) {
int[][] C = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int[] tmp = new int[n];
for (int k = 0; k < n; k++) {
tmp[k] = product(A[i][k], B[k][j]);
}
C[i][j] = sum(tmp);
}
}
return C;
}
static int product(int a, int b) {
return (a * b == 0) ? 0 : 1;
}
static int sum(int[] arr) {
for (int item : arr)
if (item == 1)
return 1;
return 0;
}
}
错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at BooleanProduct.main(BooleanProduct.java:4)
我的错误:
Multiple markers at this line
- B cannot be resolved to a (ln51)
variable
- n cannot be resolved to a (ln53)
variable
- A cannot be resolved to a (ln54)
variable
最佳答案
代码块
int[][] C = booleanProduct(A, B, n);
{ // not necessary
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(C[i][j] + " ");
}
System.out.println("");
}
} // not necessary
在方法之外。您可以将其放在
main
方法(或适当的方法)中:public static void main(String[] args) {
// ...
int[][] C = booleanProduct(A, B, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(C[i][j] + " ");
}
System.out.println("");
}
}
关于java - 查找两个矩阵的 boolean 积,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22968386/