打印各种形状三角形(Triangle Printing Program)
来源:互联网 发布:java和jsp的区别 编辑:程序博客网 时间:2024/05/29 13:50
现在发现,打印各种图形还真是要动脑筋的:
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 printedby a single statement of the form System.out.print('*'); which causes the asterisks to print sideby side. A statement of the form System.out.println(); can be used to move to the next line. Astatement 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 requirethat 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 smallSystem.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(" ");elseSystem.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(" ");elseSystem.out.print("*");}System.out.println();}}}
运行结果:(注意:从Eclipse复制粘贴运行结果时,发生了错位,程序是对的)
请输入三角形的大小(整数):18
使用while循环语句打印左对齐三角形:
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************
使用for循环语句打印左对齐三角形:
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************
左对齐倒三角形:
*******************
******************
*****************
****************
***************
**************
*************
************
***********
**********
*********
********
*******
******
*****
****
***
**
*
右对齐到三角形:
******************
*****************
****************
***************
**************
*************
************
***********
**********
*********
********
*******
******
*****
****
***
**
*
右对齐三角形:
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************
- 打印各种形状三角形(Triangle Printing Program)
- 并排打印不同形状三角形(Modified Triangle Printing Program)
- 条形图打印程序(Bar Chart Printing Program)
- 【java练习】来打印各种形状的三角形吧!!(循环练习)
- Image Printing Program Based on Halftoning (半色调打印技术)
- 8-4 打印(Printing)
- Java中各种打印形状
- java打印各种三角形
- 各种数字形状打印(嵌套for循环的应用)
- 倒三角形(triangle)
- 倒三角形(triangle)
- 倒三角形(triangle)
- 倒三角形(triangle)
- 倒三角形(triangle)
- Pascal's Triangle 打印数字三角形
- 钻石图案的绘制(Diamond Printing Program)
- 基于半色调技术的图像打印程序(Image Printing Program Based on Halftoning)
- 三角形形状
- "双向链表"-数据结构算法-之通俗易懂,完全解析
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree (DFS)
- Chromium内核net模块disk cache/index file的文件存储机制
- iOS内存管理(3)-- iOS 代理要用weak修饰
- Mac adb 配置
- 打印各种形状三角形(Triangle Printing Program)
- Android获取一些常用信息
- ISO8859-1、UTF-8 与GB2312
- AngularJS 学习记录
- Java 数据类型
- 数据处理
- (概述)JavaScript权威指南笔记1
- LeetCode 113. Path Sum II
- PI中的WSDL文件的导出