用编程打出五角星

 我来答
简乱千因1S
2017-11-06 · TA获得超过2270个赞
知道大有可为答主
回答量:1003
采纳率:100%
帮助的人:215万
展开全部

c++版本

#include<iostream>
using namespace std;
void main()
{
cout<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<"*"<<endl;
cout<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<"*"<<" "<<"*"<<endl;
cout<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<"*"<<" "<<" "<<" "<<"*"<<endl;
cout<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<"*"<<" "<<" "<<" "<<" "<<" "<<"*"<<endl;
cout<<"*"<<" "<<"*"<<" "<<"*"<<" "<<"*"<<" "<<"*"<<" "<<"*"<<" "<<"*"<<" "<<"*"<<" "<<"*"<<" "<<"*"<<" "<<"*"<<endl;
cout<<" "<<" "<<" "<<" "<<" "<<"*"<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<"*"<<endl;
cout<<" "<<" "<<" "<<" "<<"*"<<" "<<" "<<"*"<<" "<<" "<<" "<<" "<<" "<<"*"<<" "<<" "<<"*"<<endl;
cout<<" "<<" "<<" "<<"*"<<" "<<" "<<" "<<" "<<" "<<" "<<"*"<<" "<<" "<<" "<<" "<<" "<<" "<<"*"<<endl;
cout<<" "<<" "<<"*"<<" "<<" "<<" "<<" "<<" "<<"*"<<" "<<" "<<" "<<" "<<"*"<<" "<<" "<<" "<<" "<<"*"<<endl;
cout<<" "<<"*"<<" "<<" "<<"*"<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<"*"<<" "<<" "<<"*"<<endl;
cout<<"*"<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<" "<<"*"<<endl;
}

JAVA版本

import java.awt.*;
import javax.swing.*;

public class WuJiaoXing extends JPanel {

private static final long serialVersionUID = 1L;

private JFrame frame = null;

private int r = 150; // 外顶点外接圆半径

private int[] x = new int[5]; // 5个X外顶点坐标

private int[] y = new int[5]; // 5个Y外顶点坐标

private int[] x_ = new int[5]; // 5个X内顶点坐标

private int[] y_ = new int[5]; // 5个Y内顶点坐标

public WuJiaoXing() {
this.math();
frame = new JFrame("五角星");
frame.getContentPane().add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setLocation(200, 200);

frame.setVisible(true);
}

private void math() {
int c = 360 / 5; // 角度
for (int i = 0; i < 5; i++) {
x[i] = (int) (Math.cos(i * c * Math.PI / 30 - Math.PI / 2) * (r) + r);
y[i] = (int) (Math.sin(i * c * Math.PI / 30 - Math.PI / 2) * (r) + r);
}
int r_ = (int) (r * Math.sin(18 * Math.PI / 180) / Math
.sin(126 * Math.PI / 180)); // 内顶点外接圆半径
for (int i = 0; i < 5; i++) {
x_[i] = (int) (Math.cos((i * c + 18) * Math.PI / 30 - Math.PI / 2)
* (r_) + r);
y_[i] = (int) (Math.sin((i * c + 18) * Math.PI / 30 - Math.PI / 2)
* (r_) + r);
}
}

public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.YELLOW);
// g.setBackground(Color.RED);
// 填充
int[] x1 = { x[0], x[2], x_[2] };
int[] y1 = { y[0], y[2], y_[2] };
int[] x2 = { x[1], x[3], x_[3] };
int[] y2 = { y[1], y[3], y_[3] };
int[] x3 = { x[2], x[4], x_[4] };
int[] y3 = { y[2], y[4], y_[4] };
g.fillPolygon(x1, y1, 3);
g.fillPolygon(x2, y2, 3);
g.fillPolygon(x3, y3, 3);

// 描边
// g.setColor(Color.BLACK);
// g.drawLine(x[0], y[0], x[2], y[2]);
// g.drawLine(x[0], y[0], x[3], y[3]);
// g.drawLine(x[1], y[1], x[3], y[3]);
// g.drawLine(x[1], y[1], x[4], y[4]);
// g.drawLine(x[2], y[2], x[4], y[4]);
// g.drawLine(x[2], y[2], x[0], y[0]);

}

public static void main(String[] args) {
new WuJiaoXing();
}
}

长荣科机电
2024-10-27 广告
直角坐标机器人,作为深圳市长荣科机电设备有限公司的明星产品之一,以其高精度、高稳定性在自动化生产线上发挥着关键作用。该机器人采用直线电机或精密导轨驱动,能在电商平台Y、Z三个直角坐标轴上实现精准定位与运动控制,广泛应用于电子装配、包装、检测... 点击进入详情页
本回答由长荣科机电提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式