为什么我的 Java 计算出的答案比我的 TI-84 Plus 计算器和 Google 计算出的答案略高,即使我使用相同的公式?

Why is my Java calculated answer slightly higher from my TI-84 Plus Calculator and Google's calculated answer even though I used the same formula?

概览

使用 DrJava,我根据外星人入侵计算了 "Estimated corn plants damaged",外星人入侵留下麦田圈 x 以英尺为单位的直径(用户输入)。我做了一个计算来确定一英亩是 43560 英尺等于 1 英亩。每毁掉一英亩土地,就会毁掉 30000 株玉米。

我要求澄清为什么我的程序得到的答案与我的计算器略有不同的输出是 "Estimated corn plants damaged."

当用户输入 80

时,我的 Java 程序的输出

估计受损的玉米植株:3462

我的计算器和Google最后计算出来的直径一样

估计受损的玉米植株:3450

我的措辞解决方案

我得到了不同的输出,因为 dAcres 变量的格式设置为四舍五入为输出的小数点后 3 位,但它用于计算的值未四舍五入。例如,当用户输入 80 作为直径时,dAcres 变量输出 0.115。但是,dAcre 变量在其计算中使用 .11539。

最后一题

如何才能让我的计算只使用 3 位小数而不是默认的小数位数?示例:0.115 而不是 0.11539?

我的代码

import java.util.*;
import java.text.*;


public class Lab2_Problem2
{
  public static final double dPI = 3.14159;

  public static void main(String[] args )
  {
    // DECLARATIONS
    // Input-capture variables
    double dDiameter;

    // Expression-result variables
    double dArea;
    double dAcres;
    double dCornPlantsDamaged;

    // Other variables
    double dRadius;
    double dRadiusSquared;

    // Instantiations
    Scanner cin = new Scanner(System.in);
    DecimalFormat dfDiameter = new DecimalFormat("#######");
    DecimalFormat dfArea = new DecimalFormat("#######.00");
    DecimalFormat dfAcres = new DecimalFormat("##0.000");
    DecimalFormat dfCornPlantsDamaged = new DecimalFormat("#####");

    // INITIALIZE 
    // INPUT
    System.out.print("Enter crop circle diameter in feet: ");
    dDiameter = cin.nextDouble();

    // PROCESSING AND CALCULATIONS
    dRadius = dDiameter / (double) 2;
    dRadiusSquared = dRadius * dRadius;
    dArea = dPI * dRadiusSquared;
    dAcres = dArea / (double) 43560;
    dCornPlantsDamaged = dAcres * (double) 30000;

    // OUTPUT
    System.out.println("Crop circle diameter: " + dfDiameter.format(dDiameter) + " feet");
    System.out.println("Square feet: " + dfArea.format(dArea));
    System.out.println("Acres: " + dfAcres.format(dAcres));
    System.out.println("Estimated corn plants damaged: " + 
dfCornPlantsDamaged.format(dCornPlantsDamaged));
  }
}

问答

  1. 如果您的计算器不支持,您为什么要计算小数点后 3 位?
    1. 因为我希望输出与正在进行的计算相匹配。

How do I make it so my calculation uses only 3 decimal places

将其乘以一个完整的整数,截去小数,然后将其除以小数

((int) (0.11539 * 1000)) / 1000.0

或者,还有 Math.round 方法

不过,您的计算器不会使用小数点后 3 位,所以我不确定您为什么要这样做

并且,为了简单起见,您可以像这样写出公式

destroyed_acres = ((pi * (d/2)^2) ft) * (acre/ft) 
ans = (corn/acre) * destroyed_acres

FWIW,wolframalpha 说 3462

https://www.wolframalpha.com/input/?i=+%28%28pi+*+%2880%2F2%29%5E2%29+square+feet%29+to+acres+*+30000

Google 也一样 (π × (80 ÷ 2)^2) × (1 ÷ 43560) × 30000

这是来自 @cricket_007 给出的答案的示例形式的工作代码。

dTruncOne = dAcres * 1000;
dTruncTwo = (int) dTruncOne * (double) 30000; // Truncated acres
dCornPlantsDamaged = dTruncTwo / (double) 1000;

dBushelsPerAcre = 211;
dCostPerBushel = 3.90;