简体   繁体   English

Java:getSlope 无法解析为变量

[英]Java: getSlope cannot be resolved to a variable

I am having trouble with printing getSlope() to the console, with any values I plug in for (x1 - x2) / (y1 - y2), Java ends up dividing by zero every time I run the program.我在将 getSlope() 打印到控制台时遇到了问题,我为 (x1 - x2) / (y1 - y2) 插入了任何值,每次运行该程序时,Java 最终都会除以零。

Slope class:坡度等级:

public class Slope {
    private int x1;
    private int x2;
    private int y1;
    private int y2;

    public Slope(int a, int b, int c, int d) {
        a = x1;
        b = x2;
        c = y1;
        d = x2;
    }

    public void setSlope(int a, int b, int c, int d) {
        x1 = a;
        x2 = b;
        y1 = c;
        y2 = d;
    }

    public int getX() {
        return (x1 - x2);
    }

    public int getY() {
        return (y1 - y2);
    }

    public void sayThis() {
        System.out.println(getX()/getY());
    }
}

testSlope Class:测试坡度类:

public class testSlope {

    public static void main(String[] args) {
        Slope slope = new Slope(10, 5, 1, 2);
        slope.sayThis(); 
    }
    //Exception in thread "main" java.lang.ArithmeticException: / by zero
    //    at Slope.getSlope(Slope.java:22)
    //    at Slope.sayThis(Slope.java:26)
    //    at testSlope.main(testSlope.java:5)

}

The problem is that your constructor is wrong.问题是你的构造函数是错误的。 It should be like this:应该是这样的:

public Slope(int a, int b, int c, int d) {
    x1 = a;
    x2 = b;
    y1 = c;
    y2 = d;
}

public class Slope {公共类斜率{

private int x1;
private int x2;
private int y1;
private int y2;

public Slope(int a, int b, int c, int d) {

    y2 = d;
    y1 = c;
    x2 = b;
    x1 = a;

    System.out.println(x1 - x2 / y1 - y2);
}

public static void main(String[] args) {
    Slope slope = new Slope(70, 80, 1, 2);
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM