首页 > 解决方案 > 我对这个公式做错了吗

问题描述

我想知道我是否在代码中的某个地方搞砸了。我使用半径 5.5 和高度 12 对其进行了测试。我的教授有不同的结果。她的出现为 1140.40,而我的出现为 1139.82。我目前正在使用 eclipse,并且有人告诉我使用 java.lang.Math.pow,因为那是指数方法。

import java.util.Scanner; 
public class AreaVolumeOfACylinder {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
    // Data Types
    double r = 0.0;
    double h = 0.0;
    double V = 0.0;
    double pi = 3.14;
    // Inputs
    System.out.println("Enter the radius of the cylinder: ");
    r = input.nextDouble();
    System.out.println("Enter the height of the cylinder: ");
    h = input.nextDouble();
    // Formula Processing
    r = java.lang.Math.pow(r, 2);
    V = pi * r * h;
    //Outputs
    System.out.printf("The volume is %.2f%n", V);
}

}

标签: java

解决方案


这可能是因为你double pi = 3.14的不够准确。尝试改用 Math.PI。


推荐阅读