5-3输出星星梯形的代码

5-3输出星星梯形的代码

是否有优化的地方?


public class StarDemo {

    public static void main(String[] args) {

// 输出星号组成的梯形

for(int i=1,j=10;i<=5;i++,j=j-2){

   //输出空格,每外循环一次空格少输出2个

   for(int m=j;m>=0;m--){

       System.out.print(" ");

   }

   //输出星号,每行输出的星号数量与行数的关系为2*i+1

   for(int k=1;k<=2*i+1;k++){

       System.out.print("*");

   }

}

}

}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

9回答
Master_M 2017-11-29 00:21:02
public class Test {
    public static void main(String[] args){
        for(int i=1;i<5;i++){
            for(int j=1;j<5-i;j++){
                System.out.print("  ");
            }
            for(int k=0;k<2*i;k++){
                System.out.print("*"+" ");
            }
            System.out.println();
        }
    }
}


吾慕课与归 2017-11-16 23:45:14

package com.imook.basic2;

import java.util.Scanner;

public class StarDemo2 {

    public static void main(String[] args) {

        int x,y;

        int hangshu,starhangshu;

        Scanner s=new Scanner(System.in);

        System.out.println("请输入星星行数:");

        starhangshu=s.nextInt();

        hangshu=2*starhangshu-1;

        for(y=1;y<=hangshu;y++){

            if(y%2!=0){

                for(x=1;x<=(2*(2+hangshu));x++){

                    if(x<=(hangshu-y)){

                        System.out.print(" ");

                    }else if(x<=((hangshu+4)+y)){

                        System.out.print("*");

                        System.out.print(" ");

                        x++;

                        }

                    }

            }else{

                System.out.println();

            }

         }


    }


}


薛定谔的喵啊喵 2017-10-10 12:22:13

// 输出星号组成的梯形

for(int i = 1; i <= 5; i++) {

for(int j = 1; j <= 5-i; j++) {

System.out.print(" ");

}

for (int j = 1; j <= 2*i+1; j++) {

System.out.print("*");

}

System.out.println();

}


oreooooo 2017-08-17 22:34:18
shendaishn 2017-07-20 22:14:38

public class StarDemo {

    public static void main(String[] args) {

// 输出星号组成的梯形

    for(int i = 1;i <=5;i++){

       for(int j = 5;j > i;j--){

           System.out.print(" ");

       }

       for(int k = 1;k <= (i*2 +1);k++){

           System.out.print("* ");

       }

       System.out.println();

    }

}

}


木子雨 2017-07-14 09:59:11

for(int i=1;i<=5;i++){

        for(int k=1;k<=5-i;k++){

        System.out.print("  ");

        }

        for(int j=1;j<=2*i+1;j++){

        System.out.print("* ");

        }

        System.out.println();

        }

微醉的阳光 2017-03-18 14:43:48

public class StarDemo {
    public static void main(String[] args) {
        // 输出星号组成的梯形
        for(int i=0;i<5;i++){
            for(int j=4-i;j>=1;j--){
                System.out.print(" ");
            }
            for(int k=1;k<=2*(i+1)+1;k++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

慕盖茨3074476 2017-02-26 01:16:16
package com.deom;

public class Start2 {
	public static void main(String[]arge){
		 int a=1;
		 int b=1;
		 int c=1;
		 for(a=3;a<=12;a++){
			 for(c=a;c<=12;c++){
				 System.out.print(" ");
			 }
			 for(b=1;b<=a;b++){
				 System.out.print("*"+" ");
			 }
			 System.out.println();
		 }
	}

}


提问者 慕粉3402018 2017-02-22 16:55:46

噢~我忘记换行了

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星

相似问题

登录后可查看更多问答,登录/注册

请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师