Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的配方有问题吗?_Java_Math - Fatal编程技术网

Java 我的配方有问题吗?

Java 我的配方有问题吗?,java,math,Java,Math,我的代码使用父母的身高计算孩子成年后的平均身高 当母亲和父亲在4-6英尺之间时,我得到的孩子身高是7英尺以上 Hmale_child=((Hmother*13/12)+Hfather)/2 Hfemale_child=((Hfather*12/13)+Hmother)/2 加上一些括号,它只是将dad高度除以2 childHeightInch = (((((monHeightInch + (monHeightFeet * 12)) * 13) / 12) + (dadHeightInch + (

我的代码使用父母的身高计算孩子成年后的平均身高

当母亲和父亲在4-6英尺之间时,我得到的孩子身高是7英尺以上

Hmale_child=((Hmother*13/12)+Hfather)/2

Hfemale_child=((Hfather*12/13)+Hmother)/2


加上一些括号,它只是将dad高度除以2

childHeightInch = (((((monHeightInch + (monHeightFeet * 12)) * 13) / 12) + (dadHeightInch + (dadHeightFeet * 12))) / 2);

childHeightInch = (((((dadHeightInch + (dadHeightFeet * 12)) * 12) / 13) + (monHeightInch + (monHeightFeet * 12))) / 2);

如果你试着在一行上做所有的计算,那么不管你怎么做,你最终都会得到太多的括号。结果是很难准确地阅读代码,并且会出错。正如你所做的那样

不要写任何你在数学上不需要的括号。并考虑首先计算妈妈和爸爸的总高度,如下所示。

int dadsHeightTotal = dadsHeightInches + dadsHeightFeet * 12;
int mumsHeightTotal = mumsHeightInches + mumsHeightFeet * 12;

if (gender.equalsIgnoreCase("male")) {
    childsHeightTotal = ( mumsHeightTotal * 13 / 12 + dadsHeightTotal ) / 2;
} else {
    childsHeightTotal = ( dadsHeightTotal * 12 / 13 + mumsHeightTotal ) / 2;
}

childsHeightFeet = childsHeightTotal / 12;
childsHeightInches = childsHeightTotal % 12;

如果你使用整数数学,会有一些小的误差。也许你应该使用双倍。当计算以厘米为单位时,也会使用数字13,因为他是以英寸为单位,他需要将13厘米转换为英寸(~5.11英寸)@ItsMeNaira不,公式中的数字是无量纲的。他用英寸还是厘米测量都是13。为什么它们是无量纲的?如果你搜索如何估算儿童身高,它会特别注明“加/减(基于性别)5英寸或13厘米”。@ItsMeNaira如果你不相信我,试试看。如果你在第一个配方奶粉中用5代替13,你会得到很小的孩子。如果你在第二个公式中用5代替13,你会得到巨人。如果替换两个公式中的12和13,则这两个更改会相互抵消。
int dadsHeightTotal = dadsHeightInches + dadsHeightFeet * 12;
int mumsHeightTotal = mumsHeightInches + mumsHeightFeet * 12;

if (gender.equalsIgnoreCase("male")) {
    childsHeightTotal = ( mumsHeightTotal * 13 / 12 + dadsHeightTotal ) / 2;
} else {
    childsHeightTotal = ( dadsHeightTotal * 12 / 13 + mumsHeightTotal ) / 2;
}

childsHeightFeet = childsHeightTotal / 12;
childsHeightInches = childsHeightTotal % 12;