有人可以幫我找出錯誤嗎?我似乎無法弄清楚這個主代碼有什麼問題。爲了補充細節,我會給出第二個。我收到的錯誤是:StackOverflowError:null
java.lang.StackOverflowError:
null
主要代碼:
public class Line
{
private int x1, y1, x2, y2;
private double Slope;
public Line(int a1, int b1, int a2, int b2)
{
Line test = new Line(a1, b1, a2, b2);
}
public void setCoordinates(int a1, int b1, int a2, int b2)
{
a1=x1;
b1=y2;
a2=x2;
b2=y2;
}
public void calculateSlope()
{
Slope = (x2-x1)/(y2-y1);
Slope = (double)Slope;
}
public void printSlope()
{
System.out.printf("The slope is %.2f" , Slope);
}
}
二級密碼:
public class LineRunner
{
public static void main(String[] args)
{
Line test = new Line(1, 9, 10, 11);
test.calculateSlope();
test.printSlope();
test= new Line (1, 7, 18, 3);
test.calculateSlope();
test.printSlope();
test = new Line(6, 4, 2, 2);
test.calculateSlope();
test.printSlope();
test = new Line(4, 4, 5, 3);
test.calculateSlope();
test.printSlope();
test = new Line(1, 1, 2, 9);
test.calculateSlope();
test.printSlope();
}
}
請在發佈的代碼中擺脫所有不必要的和分散注意力的空白。一個空白行在任何一個地方都綽綽有餘。 –
這不是你得到的錯誤。 Java錯誤伴隨着一道信息牆。給我們那面牆,或者至少是那面牆的開始。 – Teepeemm
雖然給出的答案是正確的,但我想指出的是,您的'calculateSlope'方法將執行整數除法,因此不會像您想要的那樣產生精確的數字。將它分配給「(x2-x1)/(double)(y2-y1)」應該給你一個更精確的數字,而不是分配斜率變量兩次。 –