简体   繁体   中英

Distance between 2 locations method

For this code, I am trying to determine the distance between (x1, y1) and (x2, y2). The equation for the distance is sqrt(x2 - x1)^2 + (y2 - y1)^2.

The code looks like this,

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

I keep getting an error, CoordinateGeometry.java:23: error: ')' expected poinsDistance = Math.sqrt(Math.pow(x2 - x1, 2) + (Math.pow(y2 - y1, 2)); ^ 1 error

What does this error mean?

Also an example would be, for points (1.0, 2.0) and (1.0, 5.0), pointsDistance is 3.0.

You are missing closing ) at the end of line

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

Or remove the opening ( before Math.pow .

Your code should look like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM