有没有人能告诉我为什么我的算术运算得了零?我试图解决这个算术问题,它给出了x和y的值,但它给了我java.lang.arithmeticException错误,或者显示为零结果。这对我真的很有帮助。
这是我的输入a=6、b=10、c=8、d=12、e=800、f=900
**这个获得x和y的值的线性方程可以用以下公式求解
X= (ed -fb)/(ad -bc),y= (fa -ec)/(ad - bc)**
这就是我正在努力解决的问题。
public class linearequation {
public static void main(String[] args){
Scanner scn= new Scanner(System.in);
linear lin1 = new linear(scn.nextInt(),scn.nextInt(),scn.nextInt(),scn.nextInt(),scn.nextInt(),scn.nextInt());
if(lin1.isSolvable()) {
System.out.println(lin1.getx());
System.out.println(lin1.gety());
}else {
System.out.println("No Solution");
}
}
}
class linear {
private int a, b, c, d, e, f;
int x, y;
int den = ((a * d) - (b * c));
public linear(int na, int nb, int nc, int nd, int ne, int nf) {
na = a;
nb = b;
nc = c;
nd = d;
ne = e;
nf = f;
}
public int geta() {
return a;
}
public int getb() {
return b;
}
public int getc() {
return c;
}
public int getd() {
return d;
}
public int gete() {
return e;
}
public int getf() {
return f;
}
public int getx() {
return x = ((e * d) - (f * b)) / den;
}
public int gety() {
return y = ((f * a) - (e * c)) / den;
}
public boolean isSolvable() {
if (den <= 0) {
return false;
} else {
return true;
}
}
}```发布于 2021-02-15 15:59:37
据我所知,linear的构造函数就是问题所在。
当你实际上想要以另一种方式赋值时,你可以将na, nb, nc, nd, ne, nf传递给它,并将它们分别重新赋值为a, b, c, d, e, and f的值,例如a = na而不是na = a。
另外,在通过构造函数设置a...f的值之前设置den。den永远不会被重新分配,因此保持为0。
你的构造函数应该是这样的:
public linear(int na, int nb, int nc, int nd, int ne, int nf) {
a = na;
b = nb;
c = nc;
d = nd;
e = ne;
f = nf;
den = ((a * d) - (b * c));
}https://stackoverflow.com/questions/66204268
复制相似问题