gpt4 book ai didi

java - 使用Math类来计算

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

编写一个JAVA程序,计算圆柱体的表面积,C= 2( π r2) + 2 π r h ,其中r是圆柱体的半径,h是圆柱体的高度。允许用户输入半径和高度。将输出格式化为小数点后三位。使用 Math 类中的常量 PI 和方法 pow()。

这是我到目前为止所做的,但它无法运行。谁能帮我吗?

import Java.util.Scanner;

public class Ex5 {

public static void main (String[] args)
{
Scanner input=new Scanner(System.in);
double radius,height,area;
System.out.print("Please enter the radius of the Cylinder");
radius = input.nextDouble();
System.out.print("Please enter the height of the Cylinder");
height = input.nextDouble();
area = (4*Math.PI*radius)+2*Math.PIMath.POW(radius,2);
System.out.println("The surface of the cylinder" + area);
}
}

最佳答案

如果这是逐字源代码,那么这里有很多问题。

首先,该行:

2*Math.PIMath.POW

您忘记在 PI 和 Math 之间添加星号,因此编译器将 PIMath 视为符号。最好加上括号来查看:

2*(Math.PI)*(Math.pow(radius, 2))

请注意,Math.PI 是静态引用的常量。另一方面,Math.pow 是一种方法。

Java 中的所有标识符都区分大小写。 Math.pow 存在,Math.POW 不存在。

此外,我注意到您根本没有使用高度变量,因此您的公式计算不正确。

关于java - 使用Math类来计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2481443/

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