您的位置:首页 > 其它

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

2016-06-22 01:14 751 查看
如果不是困了,应该能在半小时内搞定。可实际花费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 from
the four separate triangles of asterisks such that all four patterns print side by side. [Hint: Make clever
use 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("*");
else
System.out.print(" ");
}
System.out.println();

}
}
}

 

运行结果:

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

*                   ****************************************                   *

**                  *******************  *******************                  **

***                 ******************    ******************                 ***

****                *****************      *****************                ****

*****               ****************        ****************               *****

******              ***************          ***************              ******

*******             **************            **************             *******

********            *************              *************            ********

*********           ************                ************           *********

**********          ***********                  ***********          **********

***********         **********                    **********         ***********

************        *********                      *********        ************

*************       ********                        ********       *************

**************      *******                          *******      **************

***************     ******                            ******     ***************

****************    *****                              *****    ****************

*****************   ****                                ****   *****************

******************  ***                                  ***  ******************

******************* **                                    ** *******************

*********************                                      *********************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: