打印各种形状三角形(Triangle Printing Program)

现在发现,打印各种图形还真是要动脑筋的:

1. 发现图形的规律

2. 确定循环语句的控制变量的初始值、增量和循环条件

调试成功的一瞬间,感觉还是挺有成就感的。

 

另外发现,在本例中,使用嵌套for循环只需4行代码,而使用while循环实现同样的功能竟然用了11行代码。

看来,Java语言的while, do...while, for都各有所长啊,“一个都不能少”。

 

代码如下:

//JHTP Exercise 5.15: Triangle Printing Program
//by pandenghuang@163.com
/*(Triangle Printing Program) Write an application that displays the following patterns separately,
one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed
by a single statement of the form System.out.print('*'); which causes the asterisks to print side
by side. A statement of the form System.out.println(); can be used to move to the next line. A
statement of the form System.out.print(' '); can be used to display a space for the last two patterns.
There should be no other output statements in the program. [Hint: The last two patterns require
that each line begin with an appropriate number of blank spaces.]*/
import java.util.Scanner;

public class Test
{
public static void main(String[] args)
{
	Scanner input=new Scanner(System.in);
	System.out.print("请输入三角形的大小(整数):");
	int size=input.nextInt();
	int outterCounter=0;
	int innerCounter=0;
	
	//small to large(while statement)
	System.out.println("使用while循环语句打印左对齐三角形:");
	while(outterCounter<size){
		while (innerCounter<=outterCounter){
			System.out.print("*");
			innerCounter++;
		}
		System.out.println();
		outterCounter++;
		innerCounter=0;
	}
	
	//small to large(for statement)
	System.out.println("\n使用for循环语句打印左对齐三角形:");
	for (int i=0;i<size;i++){
		for (int j=0;j<=i;j++)
			System.out.print("*");
		System.out.println();
		}
	
	//large to small
	System.out.println("\n左对齐倒三角形:");
	for (int i=size;i>=0;i--){
		for (int j=0;j<=i;j++)
			System.out.print("*");
		System.out.println();
		}
	
	//small to large (right aligned)
	System.out.println("\n右对齐到三角形:");
	for (int i=0;i<size;i++){
		for (int j=0;j<size;j++){
			if (j<i)
				System.out.print(" ");
			else
				System.out.print("*");
		}
		System.out.println();
		}
	
	//large to small (right aligned)
	System.out.println("\n右对齐三角形:");
	for (int i=0;i<size;i++){
		for (int j=0;j<size;j++){
			if (j<size-i-1)
				System.out.print(" ");
			else
				System.out.print("*");
		}
		System.out.println();
		}
	}
}

 

运行结果:(注意:从Eclipse复制粘贴运行结果时,发生了错位,程序是对的)

请输入三角形的大小(整数):18
使用while循环语句打印左对齐三角形:
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************

使用for循环语句打印左对齐三角形:
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************

左对齐倒三角形:
*******************
******************
*****************
****************
***************
**************
*************
************
***********
**********
*********
********
*******
******
*****
****
***
**
*

右对齐到三角形:
******************
 *****************
  ****************
   ***************
    **************
     *************
      ************
       ***********
        **********
         *********
          ********
           *******
            ******
             *****
              ****
               ***
                **
                 *

右对齐三角形:
                 *
                **
               ***
              ****
             *****
            ******
           *******
          ********
         *********
        **********
       ***********
      ************
     *************
    **************
   ***************
  ****************
 *****************
******************

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值