集册 Java实例教程 二维阵列,螺旋图案构建。类型1:In

二维阵列,螺旋图案构建。类型1:In

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

583
二维阵列,螺旋图案构建。类型1:输入输出。


import java.util.Scanner;
// 来自 时 代 J a v a 公 众 号 - N o w J a v  a . c o m

/**

 * 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, SPIRAL PATTERN BUILD. TYPE 1: IN-OUT.

 *

 * 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 Pattern1

{

  public static void main( String[] args )

  {

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


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

  }

  public void printPath( int size )

  {

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

    int x = array.length / 2, y = x - 1, top = 1;//nowjava - 时代Java 提供

    boolean up = true, right = false, down = false;


    if( size % 2 != 0 )

    {

      y ++;

      x ++;

      top = 0;

    }

    

    for( int k = 1; k <= array.length * array.length; k++ )

    {

      if( up )

      {

        if( x == y )

        {

          up = false;

          right = true;

          y ++;

        }

        else

        {

          x --;

        }

        

      }

      else

      {

        if( right )

        {

          // if( x + y == array.length - 1 )

          if( x + y == ( array.length - top ) )

          {

            right = false;

            down = true;

            x ++;

          }

          else

          {

            y ++;

          }

        }

        else

        {

          if( down )

          {

            if( x == y )

            {

              down = false;

              y --;

            }

            else

            {

              x ++;

            }

          }

          else

          {

            // if( x + y == array.length - 1 )

            if( x + y == ( array.length - top ) )

            {

              up = true;

              y --;

            }

        
展开阅读全文