如何相减或相加两个坐标

问题描述 投票:0回答:2

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

例如:

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

我已经在下面编写了此代码,但是它不起作用,或者我不知道如何称呼它...

 public void subtract(Coordinates otherCoordinates) {
       new Coordinates(this.latitude - otherCoordinates.getLatitude(),
                        this.longitude - otherCoordinates.getLongitude());
 }
java methods coordinates void
2个回答
1
投票

您的代码将无法正常运行。您的减法方法应该类似于以下内容

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

0
投票

行得通。另一种方法是:

© www.soinside.com 2019 - 2024. All rights reserved.