简体   繁体   English

JAVA矩阵程序仅打印0秒而不接受输入

[英]JAVA Matrix Program printing only 0s and not accepting input

My program runs without errors. 我的程序运行没有错误。 But, skips the readInput method somehow. 但是,以某种方式跳过readInput方法。 Which results in printing only 0s. 这导致仅打印0。

Also, how do I modify my displayResult method in order to make my output more "matrix-like" in display. 另外,如何修改我的displayResult方法,以使我的输出在显示中更像“矩阵状”。

My source code below. 我的源代码如下。

import java.util.Scanner;
public class Matrices {
    int i, j, k, n = 3;
    int[][] matA = new int[n][n];
    int[][] matB = new int[n][n];
    int[][] matSum = new int[n][n];
    int[][] matProd = new int[n][n];

    public void readInput() {
        Scanner scan = new Scanner(System.in);
        for (i = 0; n < 3; i++) {
            for (j = 0; j < n; j++) {
                System.out.println("matA[" + i + "][" + j + "]");
                matA[i][j] = scan.nextInt();
                System.out.println("matA[" + i + "][" + j + "]");
                matA[i][j] = scan.nextInt();
            }
        }
    }

    public void findSum() {
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                matSum[i][j] = matA[i][j] + matB[i][j];
            }
        }
    }

    public void findProduct() {
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                for (k = 0; k < n; k++) {
                    matProd[i][j] = matProd[i][j] + matA[i][j] * matB[i][j];
                }
            }
        }
    }

    public void displayResult() {
        // Printing the Sum Matrix
        System.out.println("Sum Matrix is:");
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                System.out.println(matSum[i][j] + " ");
            }
        }

        // Printing the Product Matrix
        System.out.println("Product Matrix is:");
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                System.out.println(matProd[i][j] + " ");
            }
        }
        System.out.println();
    }

public static void main(String[] args){

    System.out.println("Matrix Calculator");
    System.out.println("-------------------\n");
    Matrices self = new Matrices();

    self.readInput();
    self.findSum();
    self.findProduct();
    self.displayResult();
}
}

Your read input method is not working because of this: 您的读取输入方法因此无效:

 for (i = 0; n < 3; i++) {

since n is declared and init to 3 the condition n < 3 returns false, so the matrix is never filled, and everything after that is jsut based on a zero matrix! 因为n被声明并且init被初始化为3,条件n <3返回false,所以矩阵永远不会被填充,之后的所有内容都是基于零矩阵的jsut!

You did a mistake here in for loop 你在for循环中犯了一个错误

int i, j, k, n=3;
int[][]matA = new int [n][n];
int[][]matB = new int [n][n];
int[][]matSum = new int [n][n];
int[][]matProd = new int [n][n];

public void readInput(){
    Scanner scan = new Scanner(System.in);
    for(i = 0; n < 3; i++){
        for(j = 0; j < n; j++){ ...

here n<3 will evaluate to 3<3 which logically returns false and control never goes inside of first for loop and neither your nextInt gets executed. 这里n <3将评估为3 <3 ,逻辑上返回false并且控制永远不会进入第一个for loop并且你的nextInt都不会被执行。

so replace n<3 with i<3 所以用i <3代替n <3

Please use the below code: 请使用以下代码:

import java.util.Scanner;

public class Matrices {

    int i, j, k, n = 3;
    int[][] matA = new int[n][n];
    int[][] matB = new int[n][n];
    int[][] matSum = new int[n][n];
    int[][] matProd = new int[n][n];

    public void readInput() {
        Scanner scan = new Scanner(System.in);
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                System.out.println("matA[" + i + "][" + j + "]");
                matA[i][j] = scan.nextInt();
                System.out.println("matB[" + i + "][" + j + "]");
                matB[i][j] = scan.nextInt();
            }
        }
    }

    public void findSum() {
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                matSum[i][j] = matA[i][j] + matB[i][j];
            }
        }
    }

    public void findProduct() {
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                for (k = 0; k < n; k++) {
                    matProd[i][j] = matProd[i][j] + matA[i][j] * matB[i][j];
                }
            }
        }
    }

    public void displayResult() {
        // Printing the Sum Matrix
        System.out.println("Sum Matrix is:");
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                System.out.println(matSum[i][j] + " ");
            }
        }

        // Printing the Product Matrix
        System.out.println("Product Matrix is:");
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                System.out.println(matProd[i][j] + " ");
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {

        System.out.println("Matrix Calculator");
        System.out.println("-------------------\n");
        Matrices self = new Matrices();

        self.readInput();
        self.findSum();
        self.findProduct();
        self.displayResult();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM