java - 我对这个公式做错了吗
问题描述
我想知道我是否在代码中的某个地方搞砸了。我使用半径 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。
推荐阅读
- angular - Angular - 错误 TS2769:没有与此调用匹配的重载。重载 1 of 5, '(next: null | undefined, error: (error: any)
- php - 如何在 PHP 页面底部显示来自数据库的 5 个最新结果,而不是在顶部显示?
- asp.net-core - .Net Core 5 创建订单和订单详情
- html - 我正在使用 tablepress 将图像作为链接插入并希望它在新选项卡中打开
- amazon-route53 - 将域从 Google 转移到 AWS 的问题
- javascript - 执行 React 方法 3 次并在第 3 次尝试执行操作
- sql-server - 如何提取嵌套 XML 中的节点?
- xml - 从具有嵌套不一致结构的 XML 中读取数据
- c - 警告:“从 'double' 转换为 'int',可能会丢失数据”
- postgresql - 如何解决 npx install postgraphile 错误