概述
如果不是困了,应该能在半小时内搞定。可实际花费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
* **************************************** *
** ******************* ******************* **
*** ****************** ****************** ***
**** ***************** ***************** ****
***** **************** **************** *****
****** *************** *************** ******
******* ************** ************** *******
******** ************* ************* ********
********* ************ ************ *********
********** *********** *********** **********
*********** ********** ********** ***********
************ ********* ********* ************
************* ******** ******** *************
************** ******* ******* **************
*************** ****** ****** ***************
**************** ***** ***** ****************
***************** **** **** *****************
****************** *** *** ******************
******************* ** ** *******************
********************* *********************
最后
以上就是调皮乌龟为你收集整理的并排打印不同形状三角形(Modified Triangle Printing Program)的全部内容,希望文章能够帮你解决并排打印不同形状三角形(Modified Triangle Printing Program)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复