下面是添加两个矩阵的程序:

import java.util.Scanner;
//import java.io.BufferedReader;
//import java.io.InputStreamReader;
public class addmatrix {
    int row,col;
    int v[][]=new int[100][100];

    public addmatrix(int i, int j) {
        // TODO Auto-generated constructor stub
        row=i;
        col=j;
    }
    public void display(){
        for(int d=0;d<row;d++){
            for(int e=0;e<col;e++){
                System.out.print(v[d][e]);
            }
        System.out.println(" ");
        }
    }
    public void getmat(){
        Scanner iny=new Scanner(System.in);
        for(int d=0;d<row;d++)
            for(int e=0;e<col;e++){
                System.out.println("Enter the element:");
                v[d][e]=iny.nextInt();
            }
        iny.close();
        }
    public addmatrix add(addmatrix m){
        addmatrix ans=new addmatrix(m.row,m.col);
        for(int d=0;d<m.row;d++)
            for(int e=0;e<m.col;e++){
                ans.v[d][e]=v[d][e]+m.v[d][e];
            }
        return ans;
    }

    /**
     * @param args
     */
    public static void main(String[] args)throws Exception {
        // TODO Auto-generated method stub
        System.out.println("Enter the no. of rows: ");
        Scanner inp=new Scanner(System.in);
        int i=0,j=0;
        i=inp.nextInt();
        System.out.println("Enter the no. of column: ");
        j=inp.nextInt();
        addmatrix m1=new addmatrix(i,j);
        m1.getmat();
        m1.display();
        addmatrix m2=new addmatrix(i,j);
        m2.getmat();
        System.out.print("+");
        m2.display();
        addmatrix m3=new addmatrix(i,j);
        m3=m1.add(m2);
        System.out.print("=");
        m3.display();
    }

}


当第一次使用m1调用getmat()时,没有发生异常,但是当我再次创建对象m2时,如果我使用m2调用getmat(),它将引发异常。

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at addmatrix.getmat(addmatrix.java:26)
    at addmatrix.main(addmatrix.java:54)


我不明白为什么会发生这种异常。

最佳答案

在您的getmat函数中,关闭扫描仪。这也将关闭基础源,在您的情况下为System.in。由于您关闭了System.in,因此无法再读取它。

我建议您仅创建一个Scanner并将其用于所有getmat调用。

另外,请使用驼峰箱!

您可以这样进行:

import java.util.Scanner;

public class AddMatrix {
    int row, col;
    int values[][];

    public AddMatrix(int i, int j) {
        row = i;
        col = j;
        this.values = new int[i][j];
    }

    public void display() {
        for (int d = 0; d < row; d++) {
            for (int e = 0; e < col; e++) {
                System.out.print(values[d][e]);
            }
            System.out.println(" ");
        }
    }

    public void getMat(Scanner scanner) {
        for (int d = 0; d < row; d++)
            for (int e = 0; e < col; e++) {
                System.out.println("Enter the element:");
                values[d][e] = scanner.nextInt();
            }
    }

    public AddMatrix add(AddMatrix m) {
        AddMatrix ans = new AddMatrix(m.row, m.col);
        for (int d = 0; d < m.row; d++)
            for (int e = 0; e < m.col; e++) {
                ans.values[d][e] = values[d][e] + m.values[d][e];
            }
        return ans;
    }

    public static void main(String[] args) throws Exception {
        System.out.println("Enter the no. of rows: ");
        Scanner scanner = new Scanner(System.in);
        int i = 0, j = 0;
        i = scanner.nextInt();
        System.out.println("Enter the no. of column: ");
        j = scanner.nextInt();
        AddMatrix m1 = new AddMatrix(i, j);
        m1.getMat(scanner);
        m1.display();
        AddMatrix m2 = new AddMatrix(i, j);
        m2.getMat(scanner);
        System.out.print("+");
        m2.display();
        AddMatrix m3 = new AddMatrix(i, j);
        m3 = m1.add(m2);
        System.out.print("=");
        m3.display();
        scanner.close();
    }
}


注意:
我还将您的数组初始化更改为完全输入的大小,而不是100x100。

10-07 20:33