gpt4 book ai didi

java - 为什么我在下面的代码中将数字 320.04 打印到控制台?

转载 作者:太空宇宙 更新时间:2023-11-04 09:17:29 25 4
gpt4 key购买 nike

我读了很多次下面的代码,但我不明白为什么我在控制台上得到 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.0410.0 英尺 6.0 英寸 = 320.04 厘米

进程已完成,退出代码为 0

最佳答案

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

关于java - 为什么我在下面的代码中将数字 320.04 打印到控制台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58795610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com