根据输入的数字输出底边为该数字的实心菱形和对应的空心菱形

package com.test;

import java.util.Scanner;

/**
 * @date:2018-4-16
 * @author x_san
 * @describe: 根据输入的数字输出底边为该数字的实心菱形和对应的空心菱形
 *
 */
public class SolidAndHollowDiamond
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("请输入数字");
		int num = input.nextInt();
		// 实心菱形(行*123):实心正三角
		for (int i = 1; i <= num; i++)
		{
			for (int j = num - i; j > 0; j--)
			{
				System.out.print(" ");
			}
			for (int k = 1; k <= i; k++)
			{
				System.out.print("* ");
			}
			System.out.println(); // 換行
		}
		// 实心反三角
		for (int i = 1; i <= num - 1; i++)
		{
			for (int k = 1; k <= i; k++)
			{
				System.out.print(" ");
			}
			for (int j = num - i; j > 0; j--)
			{
				System.out.print("* ");
			}
			System.out.println(); // 換行
		}
		System.out.println();
		// 空心菱形(行*135):空心正三角
		for (int i = 1; i <= num; i++)
		{
			for (int j = 1; j <= num - i ; j++)
			{
				System.out.print(" ");
			}
			for (int k = 1; k <= 2 * i - 1; k++)
			{
				if (k == 1 || k == 2 * i - 1)
				{
					System.out.print("*");
				}
				else
				{
					System.out.print(" ");
				}				
			}
			System.out.println(); // 換行
		}
		// 空心反三角
		for (int i = 1; i <= num - 1; i++)
		{
			for (int k = 1; k <= i; k++)
			{
				System.out.print(" ");
			}
			for (int j = 1; j <= 2 * (num - 1 - i) + 1; j++)
			{
				if (j == 1 || j == 2 * (num - 1 - i) + 1)
				{
					System.out.print("*");
				}
				else
				{
					System.out.print(" ");
				}
			}
			System.out.println(); // 換行
		}	
	}
}

你可能感兴趣的:(Java编程题)