求圆柱的体积

题目描述

在这里插入图片描述

代码

package extendstest;

public class Circle {
	private double radius;
	
	public Circle() {
		radius=1.0;
	}

	public double getRadlus() {
		return radius;
	}

	public void setRadlus(double radlus) {
		this.radius = radlus;
	}
	//返回面积
	public double findArea() {
		return Math.PI*radius*radius;
	}
	
}




public class Cylinder extends Circle{
	private double length;
	
	public Cylinder(){
		length=1.0;
	}
	
	public void setLength(double length) {
		this.length=length;
	}
	public double getLength() {
		return length;
	}
	public double findVolume() {
		//return Math.PI*getRadlus()*getRadlus()*getLength();
		return findArea()*getLength() ;
	}
}




public class CylinderTest {

	public static void main(String[] args) {
		Cylinder cy = new Cylinder();
		
		cy.setRadlus(2.1);
		cy.setLength(3.4);
		System.out.println("圆柱的体积为"+cy.findVolume());
	}
}


结果
结果图

### 回答1: #include <stdio.h> int main() { float r, h, volume; printf("请输入圆柱的半径和高:"); scanf("%f%f", &r, &h); volume = 3.1415926 * r * r * h; // 计算圆柱体积 printf("圆柱体积为:%.2f\n", volume); return 0; } ### 回答2: 下面是一个用C语言编写的圆柱体积的程序: ```c #include <stdio.h> #define PI 3.1415926535 double calculateVolume(double radius, double height) { double volume = PI * radius * radius * height; return volume; } int main() { double radius, height; printf("请输入圆柱体的底面半径:"); scanf("%lf", &radius); printf("请输入圆柱体的高:"); scanf("%lf", &height); double volume = calculateVolume(radius, height); printf("圆柱体体积为:%.2lf\n", volume); return 0; } ``` 运行程序后,会先提示用户输入圆柱体的底面半径和高,然后根据输入的半径和高调用`calculateVolume`函数计算圆柱体体积,并将结果打印出来。 其中,`calculateVolume`函数用来计算圆柱体体积,参数`radius`表示底面半径,参数`height`表示高。根据圆柱体体积的公式,计算出体积后将其返回。 注意,代码中使用了宏定义`#define PI 3.1415926535`,将圆周率定义为一个常量方便计算。另外,`%.2lf`用来控制打印出来的体积保留两位小数。 ### 回答3: 下面是一个用C语言编写的圆柱体积的程序: ```c #include <stdio.h> #define PI 3.14159 int main() { float radius, height, volume; printf("请输入圆柱的半径和高度:"); scanf("%f%f", &radius, &height); volume = PI * radius * radius * height; printf("圆柱体体积为:%.2f\n", volume); return 0; } ``` 解释: 1. 首先,我们使用`#define`定义了一个宏常量`PI`,其值为3.14159,表示圆周率π。 2. 接着,在`main`函数中声明了三个浮点型变量`radius`、`height`和`volume`,分别用于存储圆柱的半径、高度和体积。 3. 程序通过`printf`函数打印提示信息,要用户输入圆柱的半径和高度。 4. 然后,使用`scanf`函数从用户输入的数据中读取圆柱的半径和高度,并将它们分别存储到`radius`和`height`变量中。 5. 下一步,我们使用圆柱体积的公式`volume = PI * radius * radius * height`计算圆柱体积,并将结果存储到`volume`变量中。 6. 最后,使用`printf`函数将计算得到的圆柱体积打印出来,并使用`%.2f`的格式控制符保留两位小数。 这个程序可以解任意圆柱体积,用户只需要输入正确的半径和高度即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值