我阅读了以下代码很多次,无法弄清楚为什么在控制台上得到320.04。有人可以帮我弄清楚吗?

package com.heshanshivantha;

public class Main {

    public static void main(String[] args) {

        calcFeetAndInchesToCentimeters(126);
    }

    public static double calcFeetAndInchesToCentimeters(double feet, double inches) {
        if (feet >= 0 && inches >= 0 && inches <= 12) {
            System.out.println(feet * 12 * 2.54 + inches * 2.54);
            double centimeters = feet * 12 * 2.54 + inches * 2.54;
            System.out.println(feet + " feet " + inches + " inches = " + centimeters + " cm");
            return centimeters;
        } return -1;
    }

   public static double calcFeetAndInchesToCentimeters(double inches) {
        if (inches >= 0) {
            int feet =(int) (inches / 12);
            int remainingInches =(int) (inches % 12);
            System.out.println(inches + " inches is equal to " + feet + " feet and " + remainingInches + " inches");
            return calcFeetAndInchesToCentimeters(feet, remainingInches);
        } return -1;
   }

}


126.0英寸等于10英尺和6英寸
320.04
10.0英尺6.0英寸= 320.04厘米

流程结束,退出代码为0

最佳答案

这是因为if条件之后的第一行。
删除System.out.println(feet * 12 * 2.54 + inches * 2.54);即可完成。

10-01 13:37