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

本文介绍了如何使用Java编程打印特定的图形模式,通过分析规律,确定每行的空格和星号数量,并利用二维数组记录图案,然后进行正向和反向打印,实现上下对称的图形展示。
摘要由CSDN通过智能技术生成

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 apply to the second half.but the sequence is reverse.)

public class Test {

public static void main(String[] args) {

int n = 7;

int m = (n + 1) / 2;     /*m说明头4行应怎么画*/

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

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

}

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

System.out.print("*");

b[i - 1] = b[i - 1] + "*";

} //“*”

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

System.out.print(" ");

b[i - 1] = b[i - 1] + " ";

}

System.out.print("\n");

}

/*下一段话是反向打印,下面的图案*/

for (int i = n - m; i > 0; i--) {

System.out.print(b[i - 1]);

System.out.print("\n");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值