不多说,希望能积累技术分,分享近期自己写的一些代码。
- 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(); //空心金字塔
- }
- }
复制代码 |
|