我正在研究一個非常簡單的點類,但我收到一個錯誤消息,我無法確定發生的字符串/雙重問題的位置或解決方法。非法參數異常
public String getDistance (double x1,double x2,double y1,double y2) {
double X= Math.pow((x2-x1),2);
double Y= Math.pow((y2-y1),2);
double distance = Math.sqrt(X + Y);
DecimalFormat df = new DecimalFormat("#.#####");
String pointsDistance = (""+ distance);
pointsDistance= df.format(pointsDistance);
return pointsDistance;
}
和測試代碼
double x1=p1.getX(),
x2=p2.getX(),
y1=p1.getY(),
y2=p2.getY();
pointsDistance= p1.getDistance(x1,x2,y1,y2);
編輯
我忘了補充錯誤我收到:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Number
at java.text.DecimalFormat.format(Unknown Source)
at java.text.Format.format(Unknown Source)
at Point.getDistance(Point.java:41)
at PointTest.main(PointTest.java:35)
幸運的是,您不必查明錯誤,因爲您的編譯器會爲您執行該操作......定義了「p1」,「p2」和「pointsDistance」的位置?什麼類型是'p1'和'p2'?我假設'pointsDistance'是一個'String'。什麼'getX()'和'getY()'返回任何對象類型'p1'和'p2'是什麼?哪一行是'Point.java,第41行'?哪個是'PointTest.java 35行'? – nhgrif
pointsDistance = p1.getDistance(x1,x2,y1,y2); 是第35行 – user2954611
pointsDistance = df.format(pointsDistance);是41行 – user2954611