Return 在方法重载

Return in the method overloading

我是 Java 的新手,我正在自学。我在尝试方法重载时遇到了麻烦。这是代码

 public static void main(String[] args) {
        calculateScore();
        calculateScore(500);
        calculateScore("Duy", 600);
        calcFeetAndInchesToCentimetres(100, 3.5);
        calcFeetAndInchesToCentimetres(100*12 + 3.5);
    }

 public static double calcFeetAndInchesToCentimetres(double feet, double inches) {
        if (feet >= 0 && inches >= 0 && inches <= 12) {
            double footToInches = feet * 12;
            double centimetres = (inches + footToInches) * 2.54;
            System.out.println("The value in centimetres is " + centimetres + " cm.");
            return centimetres;
        } else {
            return -1;
        }
    }

    public static double calcFeetAndInchesToCentimetres(double inches) {
        if (inches >= 0){
            double inchesToFeet = inches / 12;
            double inchesRemain = inches - (inchesToFeet * 12);
            calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);
            return 0;
        } else {
            return -1;
        }

我的问题是,当我从第二种方法中获取 return 0 时,调试器显示 "missing return statement"。然后我尝试输入 return calcFeetAndInchesToCentimetres(inches);,它有效,但程序运行了大约数千次。

然后我输入return 0就一切OK了。但是我不明白为什么我不能放置 return calcFeetAndInchesToCentimetres(inches); 以及当上面的方法(带有 2 个参数)已经存在时为什么我需要一个 return 语句。如果我想在执行第二种方法时转换厘米值(仅使用 "inches" 参数),我需要做什么?

还有一件事我在这个块代码中意识到

double inchesToFeet = inches / 12;
        double inchesRemain = inches - (inchesToFeet * 12);
        calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);

inchesRemain 将为 0?但是这个方法非常有效。当我更改 inchesToFeet = inches % 12 时,它只是不显示任何内容。为什么?

应该是:

public static double calcFeetAndInchesToCentimetres(double inches) {
    if (inches >= 0){
        double inchesToFeet = inches / 12;
        double inchesRemain = inches - (inchesToFeet * 12);
        return calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);
    } else {
        return -1;
    }
}

你说你试过 return calcFeetAndInchesToCentimetres(inches); 但那只是递归调用你的方法,并且它会永远递归,因为没有停止条件。

通过方法重载,您有两种不同的方法。

  1. calcFeetAndInchesToCentimetres 接受一个参数
  2. calcFeetAndInchesToCentimetres 有两个参数

现在,当您调用 calcFeetAndInchesToCentimetres(inches); 时,您调用的是接受单个参数的函数。如果您从自身内部调用它,它将无限次地继续调用自己。这是您看到的错误。

如果您将其替换为 return calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);,这将调用另一种方法 - 带有两个参数的方法。这才是你真正想做的。

固定版本:

public static double calcFeetAndInchesToCentimetres(double inches) {
    if (inches >= 0){
        double inchesToFeet = inches / 12;
        double inchesRemain = inches - (inchesToFeet * 12);
        return calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);
    } else {
        return -1;
    }
}
public static void main(String[] args) {
    calculateScore();
    calculateScore(500);
    calculateScore("Duy", 600);
    calcFeetAndInchesToCentimetres(100, 3.5);
    calcFeetAndInchesToCentimetres(100*12 + 3.5);
}

public static double calcFeetAndInchesToCentimetres(double feet, double inches) {
    if (feet >= 0 && inches >= 0 && inches <= 12) {
        double footToInches = feet * 12;
        double centimetres = (inches + footToInches) * 2.54;
        System.out.println("The value in centimetres is " + centimetres + " cm.");
        return centimetres;
    } else {
        return -1;
    }
}
public static double calcFeetAndInchesToCentimetres(double inches) {
    if (inches >= 0){
        double inchesToFeet = inches / 12;
        double inchesRemain = inches - (inchesToFeet * 12);
        calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);
        return 0; //Here
    } else {
        return -1;
    }

如果您删除 return 0,它会显示缺少 return 语句,因为您处于 if-else 循环中。 假设您的输入英寸小于 0,那么它会进入 else 部分和 return -1 .. 但如果输入的英寸大于 0,那么它将进入 if 条件,并在到达时到达if 语句的结尾,那么 return 就没有任何内容了,所以对于 if-else 条件,if 和 else 都应该 return something.

解决这个问题的另一种方法是在 if-else 条件和 return if 之外创建一个局部变量,在 else 部分完成之后。这样它就进入了 if 或 else 的一部分代码两次,该局部变量都会有一些值 returned..

现在开始你问题的第二部分: 你的代码看起来像

line 1   public static double calcFeetAndInchesToCentimetres(double inches) {

第2行if (inches >= 0){ 第3行double inchesToFeet = inches / 12; 第4行double inchesRemain = inches - (inchesToFeet * 12); 第5行calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain); 第6行return calcFeetAndInchesToCentimetres(inches); } else { return -1; }

所以在这种情况下,你一次又一次地调用同一个方法本身。 你从第 1 行开始直到第 6 行,然后你调用相同的方法并再次开始执行,第 1 行将执行到第 6 行,然后一遍又一遍地执行第 1 行,直到所有内存丢失并且发生 Whosebug ..

最佳实践代码如下所示:

public static double calcFeetAndInchesToCentimetres(double feet, double inches) {
    double centimetres = 0.0;
    if (feet >= 0 && inches >= 0 && inches <= 12) {
        double footToInches = feet * 12;
        centimetres = (inches + footToInches) * 2.54;
        System.out.println("The value in centimetres is " + centimetres + " cm.");
    } else {
        centimetres = -1;
    }
    return centimetres;
}


public static double calcFeetAndInchesToCentimetres(double inches) {
    double centimeters = 0;
    if (inches >= 0) {
        double inchesToFeet = inches / 12;
        double inchesRemain = inches - (inchesToFeet * 12);
        centimeters = calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);

    } else {
        centimeters = -1;
    }
    return centimeters; //Here
}