现在发现,打印各种图形还真是要动脑筋的:
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循环语句打印左对齐三角形:
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************
左对齐倒三角形:
*******************
******************
*****************
****************
***************
**************
*************
************
***********
**********
*********
********
*******
******
*****
****
***
**
*
右对齐到三角形:
******************
*****************
****************
***************
**************
*************
************
***********
**********
*********
********
*******
******
*****
****
***
**
*
右对齐三角形:
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************