首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取java arithmeticException错误

获取java arithmeticException错误
EN

Stack Overflow用户
提问于 2021-02-15 15:42:48
回答 1查看 29关注 0票数 0

有没有人能告诉我为什么我的算术运算得了零?我试图解决这个算术问题,它给出了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)**

这就是我正在努力解决的问题。

代码语言:javascript
复制
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;
        }
    }

}```
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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的值之前设置denden永远不会被重新分配,因此保持为0

你的构造函数应该是这样的:

代码语言:javascript
复制
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));
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66204268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档