gpt4 book ai didi

java - 两个矩阵相加的简单java程序中发生NoSuchElementException?

转载 作者:行者123 更新时间:2023-12-01 10:08:21 26 4
gpt4 key购买 nike

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

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。

关于java - 两个矩阵相加的简单java程序中发生NoSuchElementException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36311723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com