集册 Java实例教程 二维阵列Zig

二维阵列Zig

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

604
二维阵列之字形路径构建。


import java.util.Scanner;//来 自 nowjava.com - 时  代  Java


/**

 * 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 ZIG-ZAG PATH 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: --.

 */

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 = 1, y = -1;

    boolean up = true;


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

    {

      if( up )/**from nowjava.com - 时代Java**/

      {

        x --;

        y ++;

      }

      else

      {

        x ++;

        y --;

      }


      if( ( x == -1 ) && ( y == array.length ) )

      {

        x += 2;

        y --;

        up = false;

      }


      if( ( x == array.length ) && ( y == -1 ) )

      {

        x --;

        y += 2;

        up = true;

      }


      if( x == -1 )

      {

        x ++;

        up = false;

      }


      if( x == array.length )

      {

        x --;

        y += 2;

        up = true;

      }


      if( y == -1 )

      {

        y ++;

        up = true;

      }


      if( y == array.length )

      {

        x += 2;

        y --;

        up = false;
展开阅读全文