并排打印不同形状三角形(Modified Triangle Printing Program)

来源:互联网 发布:node excelexport 编辑:程序博客网 时间:2024/05/22 06:41

如果不是困了,应该能在半小时内搞定。可实际花费1个半小时有余。

不过,结果还是很惊艳的,4个三角形拼成了一个大写的W(Win,爱拼才会赢)。

 

代码如下:

//JHTP Exercise 5.22: Modified Triangle Printing Program//by pandenghuang@163.com/*(Modified Triangle Printing Program) Modify Exercise 5.15 to combine your code fromthe four separate triangles of asterisks such that all four patterns print side by side. [Hint: Make cleveruse of nested for loops.]*/import java.util.Scanner;public class TrianglePrint{public static void main(String[] args){Scanner input=new Scanner(System.in);System.out.print("请输入三角形的大小(整数):");int size=input.nextInt();for (int i=0;i<size;i++){for (int j=0;j<=4*size-1;j++){if(j<=i && j<=size)System.out.print("*");else if(j>=size && j<2*size && 2*size-j>i )System.out.print("*");else if(j>=2*size && j<3*size && j-2*size>=i )System.out.print("*");else if(j>=3*size && j<4*size && 4*size-j-1<=i )System.out.print("*");elseSystem.out.print(" ");}System.out.println();}}}

 

运行结果:

请输入三角形的大小(整数):20
*                   ****************************************                   *
**                  *******************  *******************                  **
***                 ******************    ******************                 ***
****                *****************      *****************                ****
*****               ****************        ****************               *****
******              ***************          ***************              ******
*******             **************            **************             *******
********            *************              *************            ********
*********           ************                ************           *********
**********          ***********                  ***********          **********
***********         **********                    **********         ***********
************        *********                      *********        ************
*************       ********                        ********       *************
**************      *******                          *******      **************
***************     ******                            ******     ***************
****************    *****                              *****    ****************
*****************   ****                                ****   *****************
******************  ***                                  ***  ******************
******************* **                                    ** *******************
*********************                                      *********************

0 0