-3

我必须在 Java 上做一些类似谷歌地图的程序,我有以下代码:

public double CGlon(double graus, double min, double sec)
    {
        double min2;
        graus =(int) longitud;
        min = (longitud - graus)*60;
        min2 = (int) min;
        sec = (min-min2)*60;
        return(graus);
        return(min); //Unreachable point
        return(sec); //Error
    }

例如,我必须将坐标 41,234234º 转换为 41º 23' 122" 。我正在调用主程序:

public class GPS {

private static final int graus = 0;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    TempClass lloc1 = new TempClass();
    TempClass lloc2 = new TempClass();
    TempClass lloc3 = new TempClass();
    TempClass lloc4 = new TempClass();

//Inicialitzacio de les dades
    //ETSE
    lloc1.nomSet("ETSE");
    lloc1.latitudSet(41.1258048);
    lloc1.longitudSet(1.2385834);

System.out.println(lloc1.CGlat(graus, 0, 0)+" graus"+lloc1.CGlat(0, 0, 0)+" minuts y "+lloc1.CGlat(0, 0, 0)+" segons de latitud.");

问题是我在'min'变量上有一个无法访问的错误,我不知道为什么。

4

4 回答 4

2

第一return条语句停止执行该方法,因此return无法到达第二条和第三条语句。如果您尝试返回三个值,请返回一个数组:

public double[] CGlon(double graus, double min, double sec)
{
    double min2;
    graus =(int) longitud;
    min = (longitud - graus)*60;
    min2 = (int) min;
    sec = (min-min2)*60;
    return new double[] { graus, min, sec };
}
于 2014-09-29T15:02:45.123 回答
1

一个函数(或方法)只能返回一次。您希望这如何工作?:

return(graus);
return(min); //Unreachable point
return(sec); //Error

一旦第一个return被执行,函数就结束了。它产生了一个值并执行完毕。因此,语句之后的任何内容return都不会被执行。

如果要返回三个单独的值,请创建某种数据结构,将这三个值保存在一起并返回该结构的实例。它可能看起来像:

public class SomeDataStructure {
    private final double graus;
    private final double min;
    private final double sec;

    public SomeDataStructure(double graus, double min, double sec) {
        this.graus = graus;
        this.min = min;
        this.sec = sec;
    }

    // also create getters for the values
}

然后在您的代码中:

SomeDataStructure result = new SomeDataStructure(graus, min, sec);
return result;
于 2014-09-29T15:03:07.320 回答
0

Look at your code:

return(graus);
return(min);
return(sec);

When the method is executed, it returns a value to the caller when it encounters the first return statement. There is no way for the method to return any other value. Consider the calling code

double result = lloc1.CGlat(graus, 0, 0);

What value would result be assigned in your case? The first, second, or third return value? Therefore, your code does not compile.

Now, if you want to return three values from a method, what you really want to do is return a compound object. To that end, create a suitable class to hold your values:

return new Location(graus, min, sec);

where the Location class assigns these to fields that you can query from the calling code, e.g. like so:

Location result = lloc1.CGlat(graus, 0, 0);
System.out.println(result.getGraus() + "°" + result.getMinutes() + "'" + result.getSeconds() + "\"");
于 2014-09-29T15:14:14.670 回答
0

您希望该代码做什么?为什么你有 3 个返回语句?

一旦您返回一个值,该方法就不会继续执行。返回值之后还有其他语句,根据定义,返回值无效,因此出现错误。

似乎您试图返回多个值,这是不可能的。相反,将 3 个返回值包装到一个 Object 中并返回它。

于 2014-09-29T15:03:11.317 回答