今天是圣诞节,我用这三种普及度比较广的语言来打印简单的圣诞树,祝大家圣诞节快乐!
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<<"$";
}
Java
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();
}
public ChristmasTree(int height, char element) {
super();
this.height = height;
this.element = element;
}
public ChristmasTree() {
super();
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public char getElement() {
return 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);
}
}
}