2015-06-21 146 views
-1

我在將getSlope()打印到控制檯時出現問題,並且插入了(x1 - x2)/(y1 - y2)的任何值,Java最終除以零每次我運行程序。Java:getSlope無法解析爲變量

坡類:

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類:

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) 

} 
+1

錯字:'的System.out.println(getSlope())'+ 2個不同的問題被詢問 – Reimeus

+0

剛另一個評論:請尊重[Java編碼約定](http://www.oracle.com/technetwork/java/codeconvtoc-136057.html):類總是以大寫字母開頭,字段/變量應該有有意義的名稱,而不是x1,x2,y1,y2,a,b,c,d。 – Turing85

+0

@JohnStobben你編輯問題的方式使原始問題不再出現。我建議回滾到舊版本。 – Turing85

回答

1

的問題是,你的構造函數是錯誤的。它應該是這樣的:

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

公共類斜率{

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); 
} 

}