今天是圣誕節,我用這三種普及度比較廣的語言來列印簡單的圣誕樹,祝大家圣誕節快樂!
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);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/240996.html
標籤:java
上一篇:SpringBoot四大核心之starter——自定義starter
下一篇:CSDN開發者周刊第 22期:谷歌 DeepMind 第四代:不學規則就可以玩游戲;圖靈獎得主 Edmund Clarke 因感染“新冠”逝世;
