Java_输出凌形


注:网页版右上方的悬浮框有目录索引

一、代码示例

public class Test {

	/*
	 * 打印凌形方法
	 * 
	 * 参数:无
	 * 返回:无
	 */
	public void show() {
		System.out.print("请输入要打印的行数(上半部分行数):");
		int i = new Scanner(System.in).nextInt();
		
		if(i>0){
			i=i-i*2;
		}
		/*
		 * public static int abs(int a)
		 * java.lang.Math
		 * 
		 * 返回 int 值的绝对值。
		 * 如果参数为非负数,则返回该参数。
		 * 如果参数为负数,则返回该参数的相反数。 
		 * 
		 * 如果参数等于 Integer.MIN_VALUE 的值(即能够表示的最小负 int 值)
		 * 那么结果与该值相同且为负。 
		 * 
		 * 参数:a - 要确定绝对值的参数。 
		 * 返回:参数的绝对值。
		 */
		int s = Math.abs(i);
		
		// 循环输出每一行
		for (; i <= s; i++) {
			// 输出空格
			for (int j = Math.abs(i); j > 0; j--) {
				System.out.print(" ");
			}
			// 输出符号:*
			for (int j = s * 2 - Math.abs(i) * 2; j >= 0; j--) {
				System.out.print("*");
			}
			// 一行结束,换行
			System.out.println();
		}
	}

	public static void main(String[] args) {
		new Test().show();
	}
}

猜你喜欢

转载自blog.csdn.net/ice_debj/article/details/103700686