编写JAVA程序,应用for循环打印菱形

编写JAVA程序,应用for循环打印菱形 ,具体代码如下:

【方法一】

public class Demo8 {
    public static void main(String[] args) {
        for(int i=1;i<10;i++){
            for (int k = 1; k <=10-i; k++) {
                System.out.print(" ");
            }
            System.out.print("*");
            for (int j = 1; j<2*i-1; j++) {
                System.out.print(" ");
            }
            System.out.print("*");
            System.out.println();
        }
        for (int i = 9; i >=1; i--) {
            for (int k = 1; k <=10-i; k++) {
                System.out.print(" ");
            }
            System.out.print("*");
            for (int j = 1; j<2*i-1; j++) {
                System.out.print(" ");
            }
            System.out.println("*");
            
            }
        }
}

运行结果 :

编写JAVA程序,应用for循环打印菱形_第1张图片

【方法二】

public class Demo9 {
    public static void main(String[] args) {
        int j;
        int xing1;
        for(j = 1; j <= 7; j += 2) {
            for(xing1 = 7; xing1 > j - 1; xing1--) {
                System.out.print(" ");
            }

            for(xing1 = 1; xing1 <= j; xing1++) {
                System.out.print("* ");
            }

            System.out.println();
        }

        for(j = 1; j <= 5; j += 2) {
            for(xing1 = 1; xing1 < j + 3; ++xing1) {
                System.out.print(" ");
            }

            for(xing1 = 5; xing1 >= j; --xing1) {
                System.out.print("* ");
            }

            System.out.println();
        }

    }
}

运行结果

编写JAVA程序,应用for循环打印菱形_第2张图片

 

以上内容仅供个人参考

你可能感兴趣的:(Java,其他)