根据您的需求,以下是几种不同编程语言中计算圆球体积和表面积的示例代码:
Python 示例
```python
import math
def calculate_sphere_properties(radius):
volume = (4/3) * math.pi * radius3
surface_area = 4 * math.pi * radius2
return volume, surface_area
radius = float(input("请输入圆球的半径: "))
volume, surface_area = calculate_sphere_properties(radius)
print(f"圆球的体积为: {volume:.2f}")
print(f"圆球的表面积为: {surface_area:.2f}")
```
C++ 示例
```cpp
include include int main() { double radius, volume, surface_area; std::cout << "请输入圆球的半径: "; std::cin >> radius; volume = (4.0/3.0) * M_PI * std::pow(radius, 3); surface_area = 4 * M_PI * std::pow(radius, 2); std::cout << std::fixed << std::setprecision(2); std::cout << "圆球的体积为: " << volume << std::endl; std::cout << "圆球的表面积为: " << surface_area << std::endl; return 0; } ``` Java 示例 ```java import java.util.Scanner; public class SphereProperties { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入圆球的半径: "); double radius = scanner.nextDouble(); double volume = (4.0/3.0) * Math.PI * Math.pow(radius, 3); double surfaceArea = 4 * Math.PI * Math.pow(radius, 2); System.out.printf("圆球的体积为: %.2f%n", volume); System.out.printf("圆球的表面积为: %.2f%n", surfaceArea); } } ``` JavaScript 示例 ```javascript function calculateSphereProperties(radius) { const volume = (4/3) * Math.PI * Math.pow(radius, 3); const surfaceArea = 4 * Math.PI * Math.pow(radius, 2); return { volume, surfaceArea }; } const radius = parseFloat(prompt("请输入圆球的半径:")); const { volume, surfaceArea } = calculateSphereProperties(radius); alert(`圆球的体积为: ${volume.toFixed(2)}`); alert(`圆球的表面积为: ${surfaceArea.toFixed(2)}`); ``` 这些示例代码分别使用 Python、C++、Java 和 JavaScript 编程语言,根据用户输入的圆球半径计算其体积和表面积,并输出结果。您可以根据自己的编程环境和需求选择合适的示例代码。