用Python,C++,Java画圣诞树

在圣诞节,博主使用普及度较广的Python、C++、Java三种语言打印简单的圣诞树,以此向大家送上节日祝福。
部署运行你感兴趣的模型镜像

今天是圣诞节,我用这三种普及度比较广的语言来打印简单的圣诞树,祝大家圣诞节快乐!

Python

i=int(4)
j=int(1)
while i>=0:
    t=int(0)
    while t<i:
        print(" ",end='')
        t=t+1
    t=int(0)
    while t<j:
        print("*",end='')
        t=t+1
    i=i-1
    j=j+2
    print()
t=int(0)
while t<4:
    print(" ",end='')
    t=t+1
print("*",end='')    

C++

#include<iostream>
using namespace std;
int main()
{
    int i=4;
    int j=1;
    while (i>=0)
    {
        for (int f = 0;f < i; f++)
        {
            cout<<" ";
        }
        for (int f = 0; f < j; f++)
        {
            cout<<"$";
        }
        cout<<endl;
        i--;
        j+=2;
    }
    for (int i = 0; i < 4; i++)
    {
        cout<<" ";
    }
    cout<<"$";
    
} // namespace name

Java

/**2020年12月25日 上午8:40:19
 * @author Yang Jiabin 
 * @describe 画圣诞树
 * 
 */
public class ChristmasTree {

	private int height=5;//圣诞树的高
	private char element='$';//圣诞树组成元素
	private int root=1;//树根高
	
	public static void main(String[] args) {
		
		ChristmasTree christmasTree=new ChristmasTree();
		christmasTree.drawTree();
		
	}

	/**构造方法
	 * @param height
	 * @param element
	 */
	public ChristmasTree(int height, char element) {
		super();
		this.height = height;
		this.element = element;
	}

	/**构造方法
	 * 
	 */
	public ChristmasTree() {
		super();
	}

	//return height 访问器
	public int getHeight() {
		return height;
	}

	//@param height 修改器
	public void setHeight(int height) {
		this.height = height;
	}

	//return element 访问器
	public char getElement() {
		return element;
	}

	//@param element 修改器
	public void setElement(char element) {
		this.element = element;
	}

	//画一棵圣诞树
	public void drawTree() {
		int len=height-1;
		int h=1;
		while(len>=0) {
			for(int i=0;i<len;i++) {
				System.out.print(" ");
			}
			for(int i=0;i<h;i++) {
				System.out.print(element);
			}
			System.out.println();
			len--;
			h+=2;
		}
		for(int i=1;i<=root;i++) {
			for(int j=1;j<height;j++) {
				System.out.print(" ");
			}
			System.out.println(element);
		}
		
		
	}
	
}

您可能感兴趣的与本文相关的镜像

Python3.11

Python3.11

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论 15
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦西空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值