java计算由几个不同形状的图形组成的总面积。设要计算的总面积中包括有三角形(triangle)、圆(circle)和矩形(rectangle)的面积。
时间: 2023-12-29 08:05:37 浏览: 48
可以定义一个总面积的变量,然后分别计算三角形、圆和矩形的面积,并将它们累加到总面积中。例如:
```java
double totalArea = 0.0;
// 计算三角形的面积
double triangleBase = 10.0;
double triangleHeight = 5.0;
double triangleArea = 0.5 * triangleBase * triangleHeight;
totalArea += triangleArea;
// 计算圆的面积
double circleRadius = 2.0;
double circleArea = Math.PI * circleRadius * circleRadius;
totalArea += circleArea;
// 计算矩形的面积
double rectangleWidth = 8.0;
double rectangleHeight = 4.0;
double rectangleArea = rectangleWidth * rectangleHeight;
totalArea += rectangleArea;
System.out.println("总面积为:" + totalArea);
```
输出结果为:
```
总面积为:62.84955592153876
```
相关问题
javajava计算由几个不同形状的图形组成的总面积。设要计算的总面积三角形(triangle)、圆(circl e)和矩形(rectangie)的面积。
可以定义一个接口 Shape,让三角形、圆和矩形都实现这个接口,然后在一个数组中存储这些形状,遍历数组计算总面积。
示例代码:
```java
interface Shape {
double getArea();
}
class Triangle implements Shape {
private double base;
private double height;
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
public double getArea() {
return 0.5 * base * height;
}
}
class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
class Rectangle implements Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
}
public class Main {
public static void main(String[] args) {
Shape[] shapes = new Shape[3];
shapes[0] = new Triangle(3, 4);
shapes[1] = new Circle(2);
shapes[2] = new Rectangle(5, 6);
double totalArea = 0;
for (Shape shape : shapes) {
totalArea += shape.getArea();
}
System.out.println("Total area: " + totalArea);
}
}
```
输出结果:
```
Total area: 47.84955592153876
```
编写一个C++程序,用于计算正方形,三角形和圆的面积以及计算各类形状的总面积
好的,我可以为您提供一个简单的C++程序,用于计算正方形、三角形和圆的面积,并计算各类形状的总面积。代码如下:
```C++
#include <iostream>
#include <cmath>
using namespace std;
// 计算正方形面积
double squareArea(double side) {
return side * side;
}
// 计算三角形面积
double triangleArea(double base, double height) {
return 0.5 * base * height;
}
// 计算圆面积
double circleArea(double radius) {
return M_PI * radius * radius;
}
// 计算多边形面积
double polygonArea(double (*areaFunc)(double), int n, double args[]) {
double totalArea = 0;
for(int i=0; i<n; i++) {
totalArea += areaFunc(args[i]);
}
return totalArea;
}
int main() {
double side, base, height, radius;
double args[3];
cout << "请输入正方形的边长:";
cin >> side;
cout << "正方形的面积为:" << squareArea(side) << endl;
cout << "请输入三角形的底边长和高:";
cin >> base >> height;
cout << "三角形的面积为:" << triangleArea(base, height) << endl;
cout << "请输入圆的半径:";
cin >> radius;
cout << "圆的面积为:" << circleArea(radius) << endl;
args[0] = side;
args[1] = base;
args[2] = radius;
cout << "各类形状的总面积为:" << polygonArea(squareArea, 1, args) + polygonArea(triangleArea, 1, args+1) + polygonArea(circleArea, 1, args+2) << endl;
return 0;
}
```
这个程序中,我们定义了4个函数,分别用于计算正方形、三角形和圆的面积,以及计算多边形的面积。在主函数中,我们通过用户输入来获取各种形状的参数,并计算它们的面积。最后,我们通过调用polygonArea函数来计算各类形状的总面积,该函数接受一个函数指针和一组参数,用于计算多边形的面积。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)