您的位置:首页 > 编程语言 > Java开发

java中如何打印规定图案? 举例说明

2014-08-03 10:17 211 查看
9.4 print out the following pattern(打印图案)。

*

***

*****

*******

*****

***

*

提示: 1)本题上面的图案和下面的图案是一样的。所以在打印上面图案的时候,把图案一行一行的都记录在数组b[i]当中。

打印下面的图案时,直接就用上面那个数组反向 打印出来就可以了。马克-to-win

2)找一下规律,第一行左边有三个空格,中间有一个星号,右边有三个空格,第二行左边有两个空格,中间有三个

星号,右边有两个空格。所以一行由三部分组成,左中右。

左边,行号i与空格数目的函数关系式是:(7 - ((2 * i) - 1)) /2,当i等于1时,前面式子等于3,当i等于2时,前面式子等于2

中间,行号i与星号数目的函 数关系式是: (2 * i - 1),当i等于1时,前面式子等于1,当i等于2时,前面式子等于3.

右边,行号i与空格数目的函数关系式是:(7 - ((2 * i) - 1)) / 2

(hint: for the first half, the rule is 2n-1. record their pattern(the number of their * asterisk and the number of space, then applyto the second half.but the sequence is reverse.)

public class Test {

publicstatic void main(String[] args) {

int n = 7;

int m = (n + 1) /2;

String[] b = new String
; //记录用set up a Array to memorize therecords

for (int i = 0; i < n; i++) {

b[i] = ""; //清空set every head of the element is"" in order to avoid the "NULL" appeared

}

for (int i = 1; i <= m; i++) {

for (int a = 0; a < (n - ((2 * i) - 1)) / 2; a++) {

System.out.print(" ");

b[i - 1] = b[i - 1] + " "; // add to itself

。。。。。。。。。。。。。。。。。

详情请见: http://www.mark-to-win.com/JavaBeginner/JavaBeginner1_web.html#9.4
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: