集册 Java实例教程 二维数组,从右到左的模式构建。

二维数组,从右到左的模式构建。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

555
二维数组,从右到左的模式构建。
/**时代Java - nowjava.com**/

import java.util.Scanner;


/**

 * Engineered and developed by Jhonny Trejos Barrios.

 * Technology: Java.

 * Version: Java Development Kit 1.8.0_31, Standard Edition.

 * Development Environment: Sublime Text 3.


 * Additional Info.

 *

 * Source Code Target:

 *

 *      BIDIMENSIONAL ARRAY, RIGHT TO LEFT PATTERN BUILD.

 *

 * Licenses: GNU GPL v3.0, Eclipse Public License 1.0, personal not for commercial purposes.

 * Developer Contact: jtrejosb@live.com || jtrejosb@gmail.com || jtrejosb@icloud.com

 * Mobile: (+57) 317 311 3223.

 */


public class Pattern

{

  public static void main( String[] args )

  {

    System.out.print( "Array size: " );


    new Pattern().printPath( new Scanner( System.in ).nextInt() );

  }

  public void printPath( int size )

  {

    int[][] array = new int[ size ][ size ];

    int x = 0, y = -1;

    boolean right = true, left = false;


    for( int k = 1; k <= size * size; k++ )

    {/*from nowjava.com - 时代Java*/

      if( right )

      {

        y ++;

      }

      else

      {

        y --;

      }


      if( y == array.length )

      {

        x ++;

        y --;

        right = false;

        left = true;

      }


      if( y == -1 )

      {

        x ++;

        y ++;

        right = true;

        left = false;

      }


      array[ x ][ y ] = k;

    }


    
展开阅读全文