[英]matrix addition java
I wrote this matrix addition program and I dont know why but I keep getting this two errors on lines 61 and 63 and i dont want to handle the exceptions but just throwing would do 我写了这个矩阵加法程序,我不知道为什么,但是我一直在第61和63行上遇到这两个错误,我不想处理异常,但是只要抛出就可以
error: unreported exception IOException; must be caught or declared
The code of the program is as follows: 该程序的代码如下:
import java.io.*;
class Arr
{
int r,c;
int arr[][];
Arr(int r,int c)
{
this.r=r;
this.c=c;
arr=new int[r][c];
}
int[][] getMatrix()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.println("enter element");
arr[i][j]=Integer.parseInt(br.readLine());
}
}
return arr;
}
int[][] findsum(int a[][],int b[][])
{
int temp[][]=new int[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
temp[i][j]=a[i][j]+b[i][j];
return temp;
}
void putmatrix(int res[][])
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.println(res[i][j]+"\t");
}
}
System.out.println();
}
}
class Matrixsum
{
public static void main(String args[])
{
Arr obj1=new Arr(3,3);
Arr obj2=new Arr(3,3);
int x[][],y[][],z[][];
System.out.println("\nEnter matrix 1");
x=obj1.getMatrix();
System.out.println("Enter matrix 2");
y=obj2.getMatrix();
z=obj1.findsum(x,y);
System.out.println("the sum matrix is");
obj2.putmatrix(z);
}
}
Because getMatrix throws IOException which must be caught or declared as thrown by main. 因为getMatrix抛出IOException,所以必须将其捕获或声明为main抛出。
In your case the simplest solution is to declare it on main. 在您的情况下,最简单的解决方案是在main上声明它。
public static void main(String... args) throws IOException
The design of class Arr
is a bit strange. Arr
类的设计有点奇怪。 Instead of using two redundant objects obj1
and obj2
, you should condense it to one object, say one called arrProcessor. 不应使用两个冗余对象obj1
和obj2
,而应将其压缩为一个对象,比如说一个名为arrProcessor的对象。 So the calls would look like: 因此,呼叫看起来像:
x=arrProcessor.getMatrix(); y=arrProcessor.getMatrix(); z=arrProcessor.findSum(x,y); arrProcessor.putMatrix(z);
Even better, instead of using arrays, you should wrap the arrays in objects, say in a class called Matrix: 更好的是,不要使用数组,而应该将数组包装在对象中,例如在称为Matrix的类中:
public class Matrix{
int[][] values;
int numRows, numCols;
protected Matrix(){/*...*/}
public static Matrix getMatrix(int nRows, int nCols){/*...*/}
public static Matrix addMatrices(Matrix a, Matrix b) throws Exception {/*...*/}
public void print(){/*...*/}
public void plus(Matrix another) throws Exception {/*...*/}
}
So now the code would look like: 所以现在代码看起来像:
Matrix x,y,z;
x=Matrix.getMatrix(3,3);
y=Matrix.getMatrix(3,3);
z=x.plus(y);
z.print();
If you tired to write your own code with matrix, just download library I developed. 如果您厌倦了使用矩阵编写自己的代码,只需下载我开发的库。 It can add, multiply, invert, transpond, calculate determinant and solve linear systems. 它可以添加,相乘,求逆,转发,计算行列式和求解线性系统。
please check: Linear Math Library 请检查: 线性数学库
This is open source you can download the source code too and look how in works. 这是开源的,您也可以下载源代码并查看其工作原理。
hey i would suggest you this one. 嘿,我建议你这个。 http://commons.apache.org/math/userguide/linear.html http://commons.apache.org/math/userguide/linear.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.