怎么编写程序圆

时间:2025-01-17 19:15:05 游戏攻略

Python

使用 `turtle` 库绘制圆:

```python

import turtle

def draw_circle(radius):

turtle.circle(radius)

turtle.setup(800, 600)

turtle.bgcolor("white")

turtle.pensize(3)

turtle.pencolor("black")

turtle.penup()

turtle.goto(0, -100)

turtle.pendown()

draw_circle(100)

turtle.done()

```

Java

定义一个 `Circle` 类并计算面积和周长:

```java

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JPanel;

class Circle {

private double radius;

public Circle(double radius) {

this.radius = radius;

}

public double getRadius() {

return radius;

}

public double getArea() {

return Math.PI * radius * radius;

}

public double getCircumference() {

return 2 * Math.PI * radius;

}

}

public class CircleApp extends JPanel {

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Circle circle = new Circle(50);

g.drawString("圆的半径为: " + circle.getRadius(), 100, 50);

g.drawString("圆的面积为: " + circle.getArea(), 100, 100);

g.drawString("圆的周长为: " + circle.getCircumference(), 100, 150);

}

public static void main(String[] args) {

JFrame frame = new JFrame("Circle Example");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(new CircleApp());

frame.setSize(400, 300);

frame.setVisible(true);

}

}

```

C

使用 `math.h` 库计算圆的面积和周长:

```c

include

include

int main() {

double radius, area, circumference;

printf("请输入圆的半径:");

scanf("%lf", &radius);

area = PI * radius * radius;

circumference = 2 * PI * radius;

printf("圆的面积为:%lf\n", area);

printf("圆的周长为:%lf\n", circumference);

return 0;

}

```

C++

使用 `easyX` 库绘制圆:

```cpp

include

int main() {

initgraph(640, 480);

circle(200, 200, 50);

getch();

closegraph();

return 0;

}

```

这些示例展示了如何在不同的编程语言中实现圆的计算和绘制。你可以根据自己的需求和使用的编程环境选择合适的示例进行参考。