打印沙漏形

笔试中遇到的填空题

完整代码如下:

 

package cn.edu.test;



public class Demo1 {

	public static void main(String[] args) {

		int i, j, m, p, k, n = 8;

		k = n / 2;

		for (i = 0; i <= n; i++) {

			m = k - Math.abs(i - k);

			p = 2 * Math.abs(i - k);

			if (p == 0) {

				continue;

			}

			for (j = 0; j < m; j++) {

				System.out.print(" ");

			}

			for (j = 0; j < p; j++) {

				System.out.print("*");

			}

			System.out.println();

		}

	}

}

/*

 * 打印结果:

********

 ******

  ****

   **

   **

  ****

 ******

********

 */


 

 

你可能感兴趣的:(打印)