gpt4 book ai didi

java - 如何减去或添加两个坐标

转载 作者:行者123 更新时间:2023-12-02 09:07:30 24 4
gpt4 key购买 nike

我正在尝试减去和添加两个城市的两个坐标。我想用 void 方法来执行此操作。您有什么建议,我该怎么做?

例如:

我想减去 city1 (x1, y1) 的坐标与 city2(x2, y2)然后在主类中调用它。那么该怎么做呢?

我在下面编写了这段代码,但它不起作用或者我不知道如何调用它......

主类:

public class Main {

public static void main(String[] args) {

Coordinates paris = new Coordinates(48.830103, 2.562957);
Coordinates rioDeJaneiro = new Coordinates(-22.492147, -43.143827);

坐标类:

 public class Coordinates {

private double latitude;
private double longitude;

public Coordinates(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}

public double getLatitude() {
return this.latitude;
}

public double getLongitude() {
return this.longitude;
}

public void subtract(Coordinates otherCoordinates) {
new Coordinates(this.latitude - otherCoordinates.getLatitude(),
this.longitude - otherCoordinates.getLongitude());
}

最佳答案

您的代码将无法按您的预期运行。你的减法应该像下面这样

public void subtract(Coordinates otherCoordinates) {
this.latitude = this.latitude- otherCoordinates.getLatitude();
this.longitude = this.longitude- otherCoordinates.getLongitude();
}

如果纬度和经度是不可变的,那么

public Coordinates subtract(Coordinates otherCoordinates) {
return new Coordinates(this.latitude - otherCoordinates.getLatitude(),
this.longitude - otherCoordinates.getLongitude());
}

关于java - 如何减去或添加两个坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59695318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com