首页 > 解决方案 > 打印出地理位置对象的字段时出错

问题描述

import java.util.*;

public class RoadTrip
{
    ArrayList<GeoLocation> roadTrip = new ArrayList<GeoLocation>();

    double cheat = 0;

    // Create a GeoLocation and add it to the road trip
    public void addStop(String name, double latitude, double longitude)
    {
        GeoLocation loc = new GeoLocation(name + " ", latitude, longitude);
        roadTrip.add(loc);
    }

    // Get the total number of stops in the trip
    public int getNumberOfStops()
    {
        return roadTrip.size();
    }

    // Get the total miles of the trip
    public double getTripLength()
    {
        double totalDistance = 0;
        for (int i = 1; i < roadTrip.size(); i++ )
        {
            GeoLocation here = roadTrip.get(i);
            GeoLocation prev = roadTrip.get(i-1);
            totalDistance = totalDistance + here.distanceFrom(prev);
        }
        return totalDistance;
    }

    // Return a formatted toString of the trip
    public String toString()
    {
        int i = 0;
        String retVal="";
        for( Object test: roadTrip)
        {

            retVal = retVal + ( i + 1 ) + ". " + test + "\n";
            i++;
        }
        return retVal;
    }
}

当我返回 retVal 时,它返回值

  1. 粉末弹簧(-110.97168, -110.97168)
  2. 阿贡(-149.00134, -149.00134)
  3. 斑马(-84.74096, -84.74096)
  4. Hyampom(-53.2522, -53.2522)
  5. 北费尔菲尔德(47.05816, 47.05816)

什么时候应该返回

  1. 粉末弹簧 (70.47312, -110.97168)
  2. 阿贡 (-12.26804, -149.00134)
  3. 斑马 (-3.89922, -84.74096)
  4. Hyampom (84.57072, -53.2522)
  5. 北费尔菲尔德 (73.14154, 47.05816)

问题是纬度值由于某种原因等于经度值。

编辑:忘了我在弄乱代码并删除了纬度部分,把它放回去;仍然给出相同的结果

标签: java

解决方案


发现错误。一个与我一起工作的人更改了地理位置中的一些代码,但没有通知我。


推荐阅读