【问题标题】:How to write a mathematical formula in Java如何用Java编写数学公式
【发布时间】:2019-10-26 02:28:02
【问题描述】:

我正在尝试找出一种将千克(由用户输入)转换为石和磅的方法。

例如:

用户输入重量为 83.456 公斤,将其乘以 2.204622 转换为磅 = 184 磅,将 184 磅除以 14 转换为石 = 13.142 石。

使用前两位数 (13) 表示石头并将余数乘以 14 得到磅,0.142(这是余数)x 14 = 1.988 磅,或者还有其他方法可以得到这个结果吗?

因此,人的体重是 13 石和 2 磅(向上或向下取整)。

到目前为止,这是我所拥有的(有效的):

pounds = kgs*2.204622;  
System.out.printf("Your weight in pounds is: %.0f" , pounds);
System.out.print(" Ibs\n");
stone = pounds / 14
//Can't figure out how to finish the formula in code

【问题讨论】:

  • 使用long pounds = Math.round(kgs*2.204622),然后使用/除法和%余数运算符来计算石头和剩余磅数。
  • 或者,如果出于某种原因,您不想使用%long stone = pounds / 14; int lbRem = pounds - (stone * 14);
  • @Andreas 嗯,我认为人们想要floor(即向下舍入)而不是round(可以向上或向下舍入)。向下舍入后剩下的是较大单位的小数部分(在本例中为石头)。
  • @RobertDodier 问题具体说:因此人的体重是 13 石和 2 磅(向上或向下取整)。

标签: java math mathematical-optimization


【解决方案1】:

我假设您在此处使用它们之前声明了 poundsstone(即使用 float pounds;double pounds;float pounds = 某些东西),否则代码将无法编译。

一种方法是分两步完成,如下所示:

double kg = 83.456;
double pounds = kg * 2.204622;

double stonesWithDecimal = pounds / 14;

int stone = (int) stonesWithDecimal; // Strip off the decimal
long poundsWithoutStone = Math.round((stonesWithDecimal - stone) * 14); // Take the fractional remainder and multiply by 14
System.out.println("Stone: " + stone + "\nPounds: " + poundsWithoutStone);

Andreas 的建议肯定更简洁,但我想同时介绍两者,因为我不确定您对在编程中使用模数的熟悉程度如何。 这是该建议的一种实现,尽管您可以通过几种不同的方式来处理数据类型(Math.round 希望返回 long):

double kg = 83.456;
double pounds = kg * 2.204622;

int stone = (int) pounds / 14;
pounds = (double) Math.round(pounds %= 14);

System.out.println("Stone: " + stone + "\nPounds: " + pounds);

【讨论】:

  • 你说得对,我在课堂上早些时候将变量声明为double,所以对其他建议有点麻烦。您的第二个建议是解决方案,效果很好。我花了很多时间试图弄清楚,我自己解决不了,谢谢。
【解决方案2】:

如果您正在寻找可扩展的即用型库,可以考虑免费和开源库UnitOf

它为 Mass 提供了 30 多次开箱即用的转换。

示例

double kgFromPound = new UnitOf.Mass().fromPounds(5).toKilograms(); 

double poundFromKg = new UnitOf.Mass().fromKilograms(5).toPounds(); 

希望对你有帮助!

【讨论】:

    【解决方案3】:

    正确的解决方案提早轮换。这是my initial comment建议的代码:

    double kgs = 83.456;
    long pounds = Math.round(kgs*2.204622);
    System.out.println("Your weight is " + pounds / 14 + " stone and " + pounds % 14 + " pounds");
    

    输出

    Your weight is 13 stone and 2 pounds
    

    如果您改为使用 69.853 公斤,您会得到

    Your weight is 11 stone and 0 pounds
    

    但如果你不早点结束,这就是事情变得危险的地方。


    (当前接受的)answer by Lightning 中的两个解决方案都是错误的,因为它们在错误的时间进行舍入。你必须提前四舍五入是有原因的。

    如果您在这两种解决方案中改为使用 69.853 公斤,您会得到

    Solution 1:
      Stone: 10
      Pounds: 14
    
    Solution 2:
      Stone: 10
      Pounds: 14.0
    

    两者显然都不正确,因为Pounds 不应该是 14,也就是 1 石头。

    如果您在不四舍五入的情况下打印值,则舍入错误的原因会很明显

    double kgs = 69.853;
    double pounds = kgs*2.204622;
    System.out.println(pounds + " lbs = " + pounds / 14 + " stone and " + pounds % 14 + " pounds");
    

    输出

    153.99946056599998 lbs = 10.999961468999999 stone and 13.999460565999982 pounds
    

    【讨论】:

    • 很好的解释。我现在完全理解所有答案以及为什么你的答案是解决方案,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多