并排打印不同形状三角形(Modified Triangle Printing Program)
2016-06-22 01:14
751 查看
如果不是困了,应该能在半小时内搞定。可实际花费1个半小时有余。
不过,结果还是很惊艳的,4个三角形拼成了一个大写的W(Win,爱拼才会赢)。
代码如下:
运行结果:
请输入三角形的大小(整数):20
* **************************************** *
** ******************* ******************* **
*** ****************** ****************** ***
**** ***************** ***************** ****
***** **************** **************** *****
****** *************** *************** ******
******* ************** ************** *******
******** ************* ************* ********
********* ************ ************ *********
********** *********** *********** **********
*********** ********** ********** ***********
************ ********* ********* ************
************* ******** ******** *************
************** ******* ******* **************
*************** ****** ****** ***************
**************** ***** ***** ****************
***************** **** **** *****************
****************** *** *** ******************
******************* ** ** *******************
********************* *********************
不过,结果还是很惊艳的,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
* **************************************** *
** ******************* ******************* **
*** ****************** ****************** ***
**** ***************** ***************** ****
***** **************** **************** *****
****** *************** *************** ******
******* ************** ************** *******
******** ************* ************* ********
********* ************ ************ *********
********** *********** *********** **********
*********** ********** ********** ***********
************ ********* ********* ************
************* ******** ******** *************
************** ******* ******* **************
*************** ****** ****** ***************
**************** ***** ***** ****************
***************** **** **** *****************
****************** *** *** ******************
******************* ** ** *******************
********************* *********************
相关文章推荐
- Map 与 Multimap
- win平台C语言油槽通信
- PHP 实现数学问题:组合
- oracle查看字符集和修改字符集
- web应用组件化思考
- [LeetCode] Design Hit Counter 设计点击计数器
- 【Cocos2d-x 3.0 中文基础教程】精灵帧缓存
- Unity视角控制和Camera的移动
- 琐事小记
- 牛人ACM
- 深入剖析Nodejs的异步IO
- 探索 ConcurrentHashMap 高并发性的实现机制
- 继承与组合的区别
- CloudBoot部署实践
- 【读书笔记】Android的Ashmem机制学习
- HashMap工作原理
- 西安城市标识牌的缺陷
- linux中fcntl()、lockf、flock的区别
- 日常小结-内省、beanutils
- iOS开发--AVFoundation自定义相机