编写一个java程序,能够通过读取键盘输入的圆柱体的半径r和高度h,从而计算圆柱的面积和体积,并进行输出显示。
时间: 2023-04-25 13:02:49 浏览: 400
以下是Java程序:
import java.util.Scanner;
public class Cylinder {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入圆柱体的半径:");
double radius = input.nextDouble();
System.out.print("请输入圆柱体的高度:");
double height = input.nextDouble();
double area = 2 * Math.PI * radius * height + 2 * Math.PI * radius * radius;
double volume = Math.PI * radius * radius * height;
System.out.println("圆柱体的面积为:" + area);
System.out.println("圆柱体的体积为:" + volume);
}
}
运行程序后,会提示用户输入圆柱体的半径和高度,然后计算出圆柱体的面积和体积,并输出显示。
相关问题
编写一个JAVA程序,能够读取键盘输入的圆柱体的半径r和h从而计算圆柱的面积和体积,并进行输出显示。
您好,我可以回答这个问题。以下是JAVA程序的代码:
import java.util.Scanner;
public class Cylinder {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入圆柱体的半径r:");
double r = input.nextDouble();
System.out.print("请输入圆柱体的高h:");
double h = input.nextDouble();
double area = 2 * Math.PI * r * r + 2 * Math.PI * r * h;
double volume = Math.PI * r * r * h;
System.out.printf("圆柱体的面积为:%.2f,体积为:%.2f", area, volume);
}
}
感谢您的提问!
【简答题】编写一个程序,能够读取圆柱体的半径和长度,用下列计算公式计算圆柱面积和体积。
好的,根据您的要求,我可以为您编写一个能够读取圆柱体的半径和长度,并计算圆柱面积和体积的Java程序。以下是代码示例:
```
import java.util.Scanner;
public class Cylinder {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the radius of the cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter the length of the cylinder: ");
double length = input.nextDouble();
double area = radius * radius * Math.PI;
double volume = area * length;
System.out.println("The area of the cylinder is: " + area);
System.out.println("The volume of the cylinder is: " + volume);
}
}
```
程序首先使用 Scanner 类从键盘输入圆柱体的半径和长度,然后使用圆的面积公式计算圆柱的底面积,即 area = radius * radius * Math.PI。接着,我们将圆柱的底面积乘以长度,即可计算出圆柱的体积,即 volume = area * length。最后,程序输出圆柱的面积和体积。
例如,当输入半径为 2.5,长度为 5 的圆柱体时,程序的输出为:
```
Enter the radius of the cylinder: 2.5
Enter the length of the cylinder: 5
The area of the cylinder is: 19.634954084936208
The volume of the cylinder is: 98.17477042468104
```
这就是一个简单的计算圆柱面积和体积的Java程序。