小编use*_*611的帖子

非法论证例外

我正在研究一个非常简单的点类,但我收到一个错误,我无法确定String/double问题发生在哪里或如何修复它.

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;
        }
Run Code Online (Sandbox Code Playgroud)

和测试代码

double x1=p1.getX(),
                       x2=p2.getX(), 
                       y1=p1.getY(),
                       y2=p2.getY(); 

           pointsDistance= p1.getDistance(x1,x2,y1,y2);
Run Code Online (Sandbox Code Playgroud)

编辑

我忘了添加我收到的错误:

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)
Run Code Online (Sandbox Code Playgroud)

java class illegalargumentexception

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

class ×1

illegalargumentexception ×1

java ×1