对于此代码,我试图确定 (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);
}
}
我不断收到错误,CooperativeGeometry.java:23: error: ')'预期 点距离 = Math.sqrt(Math.pow(x2 - x1, 2) + (Math.pow(y2 - y1, 2)); ^ 1 个错误
这个错误是什么意思?
还有一个例子,对于点 (1.0, 2.0) 和 (1.0, 5.0),pointDistance 为 3.0。
最佳答案
您缺少行尾的结束 )
pointsDistance = Math.sqrt(Math.pow(x2 - x1, 2) + (Math.pow(y2 - y1, 2)));
或者删除 Math.pow
之前的开头 (
。
您的代码应如下所示:
pointsDistance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
关于java - 2个位置之间的距离法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54350941/