码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-09-22 18:26:31      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:www.   ace   inner   pat   部分   ever   []   url   class   

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");
        }
    }
}

结果:
   * 
  ***
 *****
*******
 *****
  ***
   * 

 


详情请见:http://www.mark-to-win.com/index.html?content=JavaBeginner/javaUrl.html&chapter=JavaBeginner/JavaBeginner1_web.html#9.4

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

标签:www.   ace   inner   pat   部分   ever   []   url   class   

原文地址:https://www.cnblogs.com/mark-to-win/p/9690367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!