日期:2014-05-20 浏览次数:20741 次
public class Test { public static void main(String[] args) { printDiamond(7, true); System.out.println(); printDiamond(7, false); } /** * 输出菱形 * @param line 菱形的行数 * @param isSolid 是否为实心 */ private static void printDiamond(int line, boolean isSolid) { line = line % 2 == 0 ? line + 1 : line; for(int k = line / 2, i = -k; i <= k; i++) { for(int j = -k, m = k - Math.abs(i); j <= m; j++) { boolean b; if(isSolid) { b = Math.abs(j) + Math.abs(i) > k; } else { b = Math.abs(j) + Math.abs(i) != k; } System.out.print(b ? " " : "*"); } System.out.println(); } } }