java 打印星号

2022/1/2 17:07:41

本文主要是介绍java 打印星号,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

代码1

package lesson.l2_for;
//6列4行
//******
//******
//******
//******
public class ForDemo8 {
    public static void main(String[] args) {
        for (int i=1;i<=4;i++){
            for (int j = 1; j <=6 ; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

代码2

package lesson.l2_for;
//打印
//*
//**
//***
//****
//*****
public class ForDemo9 {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

代码2-1

package lesson.l2_for;
//打印
//****
//***
//**
//*
public class ForDemo10 {
    public static void main(String[] args) {
        for (int i = 4; i >= 1; i--) {
            for (int j = i; j >= 1; j--) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

代码2-2

package lesson.l2_for;
//打印
//****
//***
//**
//*
public class ForDemo10_2 {
    public static void main(String[] args) {
        for (int i = 1; i <=4 ; i++) {
            for (int j = 1; j <=5-i ; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

代码3

package lesson.l2_for;
//打印
//*
//**
//***
//****
//*****
//****
//***
//**
//*
public class ForDemo11 {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();

        }
        for (int i = 1; i <= 4; i++) {
            for (int k = 1; k <= 5 - i; k++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

代码4

在这里插入图片描述

package lesson.l2_for;

//打印棱形

public class ForDemo12 {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <=6-i; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= i; k++) {
                System.out.print(" *");
            }
            System.out.println();
        }
        for (int i = 1; i <=4 ; i++) {
            for (int j = 1; j <=i+1 ; j++) {
                System.out.print(" ");
            }
            //for (int j = i; j <=4; j++) {
            for (int j = 1; j <=5-i; j++) {
                System.out.print(" *");
            }
            System.out.println();
        }
    }
}


代码5

在这里插入图片描述

package lesson.l2_for;
//打印
//        **********
//        ****  ****
//        ***    ***
//        **      **
//        *        *
//        **      **
//        ***    ***
//        ****  ****
//        **********
public class ForDemo13 {
    public static void main(String[] args) {
		//上半部分
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 6 - i; j++) {
                System.out.print("*");
            }

            for (int j = 1; j <= i - 1; j++) {
                System.out.print("  ");
            }


            for (int j = 1; j <= 6 - i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        //下半部分
        for (int i = 2; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            for (int j = 1; j <= 5 - i; j++) {
                System.out.print("  ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}



这篇关于java 打印星号的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程