为什么我在下面的代码中将数字 320.04 打印到控制台?
Why do I get number 320.04 printed to the console in the below code?
我读了下面的代码很多遍,我不明白为什么我在控制台上得到 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);
就完成了。
我读了下面的代码很多遍,我不明白为什么我在控制台上得到 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);
就完成了。