收到 java arithmeticException 错误
getting an java arithmeticException error
谁能帮我解释一下为什么我的算术运算结果为零?
我正在尝试解决这个给出 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;
}
}
}```
据我所知,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));
}
谁能帮我解释一下为什么我的算术运算结果为零? 我正在尝试解决这个给出 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;
}
}
}```
据我所知,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));
}