我想知道我的代码中是否搞砸了。我使用 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);
}
}
最佳答案
这可能是因为您的 double pi = 3.14
不够准确。尝试使用 Math.PI 代替。
关于java - 我这个公式做错了吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59870229/