【问题标题】:Java (Eclipse) Class return statements not workingJava(Eclipse)类返回语句不起作用
【发布时间】:2017-08-22 17:58:43
【问题描述】:

我正在编写一个创建访问器和修改器的类。但是,当我运行我的程序以使用该类时,没有返回任何内容,或者我的程序没有输出任何内容。

在测试程序(如下)中,我创建类变量并将它们发送到我输入的类,然后返回变量。

我的理想输出是函数调用返回变量。 例如(getLatitude 将输出纬度变量)

这是我的课程代码:

package practiceProblems;

import stdlib.StdOut;

public class GPSPosition implements Comparable <GPSPosition> {

    // Private global variables
    private Double Latitude;
    private Double Longitude;
    private Double Altitude;

    // Constructor 1
    public <Item extends Comparable<? super Item>> GPSPosition() { 
        Latitude  = 0.0; 
        Longitude = 0.0; 
    }

    // Constructor 2
    public <Item extends Comparable<? super Item>> GPSPosition (Double lat, Double lon, Double alt) {
        this.Altitude  = alt;
        this.Latitude  = lat;
        this.Longitude = lon;

        if (lat < -90 || lat > 90) {
            throw new IllegalArgumentException ("NOPE!");
        }

        if (lon < -180 || lon > 180) {
            throw new IllegalArgumentException ("NOPE!!");
        }

        if (alt < 0) {
            throw new IllegalArgumentException ("NOPE!!!");
        }
    }

    // Latitude Accessor
    public Double getLatitude () { 
        return this.Latitude; 
    }

    // Longitude Accessor
    public Double getLongitute () { 
        return this.Longitude; 
    }

    // Altitude Accessor
    public Double getAltutude () { 
       return this.Altitude; 
    }

    // Mutator for Latitude
    public void setLatitude (Double Latitude) { 
        this.Latitude = Latitude;
    }

    // Mutator for Longitude
    public void setLongitude (Double Longitude) { 
        this.Longitude = Longitude;
    }

    // Mutator for Altitude
    public void setAltitude (Double Altitude) { 
        this.Altitude = Altitude;
    }

    // Compare to method
    public int compareTo (GPSPosition that) {
        if (this.Latitude.compareTo (that.Latitude) > 0) {
            return 1;
        }

        if (this.Latitude.compareTo (that.Latitude) < 0) {
            return -1;
        }

        return that.Latitude.compareTo (this.Latitude);
    }

    // toString method
    public String toString () {
        String latOutput  = "";
        String longOutput = "";

        if (this.Latitude < 0) { 
            latOutput = "S"; 
        } else {
            latOutput = "N";
        }

        if (this.Longitude < 0) { 
            latOutput = "W"; 
        } else {
            latOutput = "E";
        }

        return (this.Latitude + latOutput + " " + this.Longitude + longOutput + " " + this.Altitude + "m");
    }

    public double distance (GPSPosition that) {
        return (Math.sqrt ((this.Latitude - this.Longitude) + (that.Longitude - that.Latitude)));
    }   
}

这是我的测试程序:

package practiceProblems;

import practiceProblems.GPSPosition;

public class TestGPS {

    public static void main (String[] args) {

        // Positions
        GPSPosition position  = new GPSPosition (-37.2, 87.2, 200.0);   
        GPSPosition position2 = new GPSPosition (37.2, 7.2, 100.0); 

        // Set
        position.setAltitude (200.0); 
        position.setLatitude (-37.2);
        position.setLongitude (87.2);

        // Get
        position.getAltutude (); 
        position.getLatitude (); 
        position.getLongitute ();

        // Compare to
        position.compareTo (position2);

        // To String call
        position.toString ();

        // Distance between two positions
        position.distance (position2);  
    }
}

【问题讨论】:

  • 你期待什么输出?
  • 所有的return语句?
  • 但是你必须为此声明变量,比如String positionStr = position.toString();
  • return 语句不会自行将内容打印到 StdOut。
  • 返回调用不打印任何内容,它们被用作函数的“结果”。 toString() 方法也不打印任何内容,它只是返回一个字符串。如果你想打印一些东西到 Eclipse 的控制台,你必须使用System.out.println()。你可以做类似System.out.println(position.toString()) 的事情。如果你想让getLatitude() 和其他方法也打印它们的返回值,你可以在return 语句之前调用System.out.println(return 语句之后的所有内容都不会执行)。

标签: java eclipse class testing return


【解决方案1】:

我建议在您的主要方法中使用以下内容:

public class TestGPS {

    public static void main(String[] args) {
        // Positions
        GPSPosition position = new GPSPosition(-37.2, 87.2, 200.0); 
        GPSPosition position2 = new GPSPosition(37.2, 7.2, 100.0);  

        // Set
        position.setAltitude(200.0); position.setLatitude(-37.2);
        position.setLongitude(87.2);

        // Get
        System.out.println("Altitude:  " + position.getAltutude()); 
        System.out.println("Latitude:  " + position.getLatitude()); 
        System.out.println("Longitude: " + position.getLongitute());

        // Compare to
        System.out.println("Comparison: " + position.compareTo(position2));

        // To String call
        System.out.println("Position: " + position.toString());

        // Distance between two positions
        System.out.println("Distance: " + position.distance(position2));   

    }

但是,请注意您的距离方法不返回数字,NaN 代表“非数字”。

【讨论】:

  • 这很有意义。我试过了,它奏效了!但是,我认为我对 return 语句的实际作用感到困惑。这就是为什么我不确定它为什么要打印的原因。
  • return 语句用于向调用代码报告返回值,取决于“return”是否有参数。如果它有一个参数,则该值将“返回”给调用者。这可能会有所帮助:docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
  • 非常感谢您的帮助!如果你不介意,最后一件事:你怎么知道我的距离方法会给我一个 NaN?我稍微改了一下代码:(double distance = Math.sqrt((this.Latitude - this.Longitude) + (that.Longitude - that.Latitude)); return distance;
  • 我没有。当我运行上面发布的代码时,我得到了 NaN。
猜你喜欢
  • 2017-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 2012-03-18
相关资源
最近更新 更多