黑马程序员技术交流社区
标题:
Pyramid星号(*)金字塔Java实现
[打印本页]
作者:
tsiov
时间:
2015-7-9 20:51
标题:
Pyramid星号(*)金字塔Java实现
不多说,希望能积累技术分,分享近期自己写的一些代码。
package test;
/*******************************
(3)请用循环
输出如下结果:
* *
*** * *
***** * *
******* * *
********* *********
*******************************/
class Draw{
final int HowBig=4; //Decides how big of the pyramid.
int st=1;
void PaintStar(){
System.out.print("*");
}
void PaintSpace(){
System.out.print(" ");
}
void NextLine(){
System.out.println();
}
void FullStars(){
st=1; //init
for(int i=HowBig;i>=0;i--){
for(int sc=0;sc<i;sc++){
PaintSpace();
}
for(int sa=0;sa<st;sa++){
PaintStar();
}
st+=2;
NextLine();
}
}
void EmptyStars(){
st=1; //init head
for(int i=HowBig;i>=0;i--){
for(int sc=0;sc<i;sc++){
PaintSpace();
}
for(int sa=0;sa<st;sa++){
if(sa==0||sa==st-1||i==0){
PaintStar();
}
else{
PaintSpace();
}
}
st+=2;
NextLine();
}
}
}
public class Pyramid {
public static void main(String ARGS[]){
Draw dr=new Draw();
dr.FullStars(); //实心金字塔
dr.EmptyStars(); //空心金字塔
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2