打印金字塔,有没有更好的方案?

打印如下:

_____1_____
____2_2____
___3_3_3___
__4_4_4_4__
_5_5_5_5_5_
6_6_6_6_6_6

 

我的代码:

		int t = 6;
		int mm = t - 1;
		int m = mm * 2 + 1;

		for (int i = 0; i < t; i++)
		{
			boolean b = false;
			int st = 0;
			int pi = i + 1;
			int count = 0;
			for (int j = 0; j < m; j++)
			{
				if (b)
				{
					if (count >= pi)
					{

					}
					else if ((j - st) % 2 == 0)
					{
						System.out.print(pi);
						count += 1;
						continue;
					}
				}
				else if (i == Math.abs(j - mm))
				{
					System.out.print(pi);
					count += 1;
					b = true;
					st = j;
					continue;
				}
				System.out.print('_');
			}
			System.out.println();
		}
	

诸君有没有更好的方案?


Total views.

© 2013 - 2024. All rights reserved.

Powered by Hydejack v6.6.1