找到两个矩阵的布尔乘积

时间:2014-04-09 16:19:39

标签: java matrix boolean

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

1 个答案:

答案 0 :(得分:1)

代码块

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("");
    }
}