打印地理位置对象的字段时出错 import java.util.*; 公务舱旅行 { ArrayList roadTrip=新的ArrayList(); 双重欺骗=0; //创建地理位置并将其添加到公路旅行中 公共void addStop(字符串名称、双纬度、双经度) { 地理位置loc=新地理位置(名称+“”,纬度,经度); 路途旅行。添加(loc); } //获取行程中的总停车次数 public int getNumberOfStops() { 返回roadTrip.size(); } //获取旅行的总英里数 公共双getTripLength() { 双总距离=0; 对于(int i=1;i

打印地理位置对象的字段时出错 import java.util.*; 公务舱旅行 { ArrayList roadTrip=新的ArrayList(); 双重欺骗=0; //创建地理位置并将其添加到公路旅行中 公共void addStop(字符串名称、双纬度、双经度) { 地理位置loc=新地理位置(名称+“”,纬度,经度); 路途旅行。添加(loc); } //获取行程中的总停车次数 public int getNumberOfStops() { 返回roadTrip.size(); } //获取旅行的总英里数 公共双getTripLength() { 双总距离=0; 对于(int i=1;i,java,Java,当我返回retVal时,它返回值 粉末弹簧(-110.97168,-110.97168) 阿贡(-149.00134,-149.00134) 泽巴(-84.74096,-84.74096) 海姆波姆(-53.2522,-53.2522) 北费尔菲尔德(47.05816,47.05816) 什么时候应该回来 粉末弹簧(70.47312,-110.97168) 阿贡(-12.26804,-149.00134) 泽巴(-3.89922,-84.74096) 海姆波姆(84.57072,-53.2522

当我返回retVal时,它返回值

  • 粉末弹簧(-110.97168,-110.97168)
  • 阿贡(-149.00134,-149.00134)
  • 泽巴(-84.74096,-84.74096)
  • 海姆波姆(-53.2522,-53.2522)
  • 北费尔菲尔德(47.05816,47.05816)
  • 什么时候应该回来

  • 粉末弹簧(70.47312,-110.97168)
  • 阿贡(-12.26804,-149.00134)
  • 泽巴(-3.89922,-84.74096)
  • 海姆波姆(84.57072,-53.2522)
  • 北费尔菲尔德(73.14154,47.05816)
  • 问题是纬度值出于某种原因等于经度值


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

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

    为什么您的
    addStop
    方法接受
    latitude
    参数,然后不处理它?大概它应该将其传递到
    GeoLocation
    constructor…?您没有将纬度传递到您的GeoLocation构造函数中。
    GeoLocation loc=new GeoLocation(name+“”,longitude)
    这里您正在构造
    loc
    对象,其中只传递两个参数
    name
    ,它是
    字符串
    经度
    ,它是
    。你在
    addStop
    方法中传递的
    latitude
    double值怎么样?我的错,我在处理代码时忘记更改代码了,现在已修复。请共享地理定位类的代码。。。
    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;
        }
    }