/** */ /** * HollowRhombus.java * Java控制台打印一个空心菱形 * @author 邓超(CodingMouse) * @version 0.1 2008-5-6 * 开发/测试环境:jdk1.6 + eclipse SDK 3.3.2 */ // 导入Scanner类 import java.util.Scanner; public class HollowRhombus ... { public static void main(String[] args) ...{ Scanner input = new Scanner(System.in); //循环标识 boolean con = true; //菱形的层数和转换计算方式后的层数 int border , num = 0; do ...{ con = false; System.out.print("请输入要打印的菱形层数[不小于3的奇(整)数]:"); //菱形的层数 border = input.nextInt(); if (border % 2 == 0 || border < 3) ...{ System.out.println("只能输入不小于3的奇(整)数!请重试!"); con = true; } } while (con); //转换层数计算方式 num = border - (border - (border % 2)) / 2; //控制台输出提示 System.out.println(border + "层空心菱形图案如下: "); // 打印上半部分图象 for (int i = 1; i <= num; i++) ...{ for (int j = 1; j <= num - i; j++) ...{ System.out.print(" "); } for (int j = 1; j <= 2 * (i + 1) - 1; j++) ...{ if (j == 1 || j == 2 * (i + 1) - 3) ...{ //System.out.print("*"); //这样瘦长一点 System.out.print("* "); } else ...{ //System.out.print(" "); //这样瘦长一点 System.out.print(" "); } } System.out.println(); } // 打印下半部分图象 for (int i = 1; i <= num - 1; i++) ...{ for (int j = 1; j <= i; j++) ...{ //System.out.print(" "); //这样瘦长一点 System.out.print(" "); } int k = num + 1 - i; for (int j = 1; j <= 2 * k - 1; j++) ...{ if (j == 1 || j == 2 * k - 3) ...{ //System.out.print("*"); //这样瘦长一点 System.out.print("* "); } else ...{ //System.out.print(" "); //这样瘦长一点 System.out.print(" "); } } System.out.println(); } }}