gpt4 book ai didi

java - 我这个公式做错了吗

转载 作者:行者123 更新时间:2023-12-02 01:07:06 25 4
gpt4 key购买 nike

我想知道我的代码中是否搞砸了。我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com