两个位置之间的距离方法

时间:2019-01-24 16:13:16

标签: java class math

对于此代码,我试图确定(x1,y1)与(x2,y2)之间的距离。距离的等式是sqrt(x2-x1)^ 2 +(y2-y1)^ 2。

代码如下:

import java.util.Scanner;

public class CoordinateGeometry {
  public static void main(String [] args) {
  Scanner scnr = new Scanner(System.in);
  double x1;
  double y1;
  double x2;
  double y2;
  double pointsDistance;
  double xDist;
  double yDist;

  pointsDistance = 0.0;
  xDist = 0.0;
  yDist = 0.0;

  x1 = scnr.nextDouble();
  y1 = scnr.nextDouble();
  x2 = scnr.nextDouble();
  y2 = scnr.nextDouble();

  poinsDistance = Math.sqrt(Math.pow(x2 - x1, 2) + (Math.pow(y2 - y1, 2));

  System.out.println(pointsDistance);
  }
}

我一直收到错误消息,CoordinateGeometry.java:23:错误:')'       poinsDistance = Math.sqrt(Math.pow(x2-x1,2)+(Math.pow(y2-y1,2));                                                                              ^ 1个错误

此错误是什么意思?

还有一个例子,对于点(1.0,2.0)和(1.0,5.0),pointsDistance是3.0。

1 个答案:

答案 0 :(得分:1)

您缺少在行尾)

poinsDistance = Math.sqrt(Math.pow(x2 - x1, 2) + (Math.pow(y2 - y1, 2)));

或删除(之前的开头Math.pow

您的代码应如下所示:

poinsDistance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));