集册 Java实例教程 二维阵列,向上

二维阵列,向上

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

467
二维数组,上下模式构建。


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, UP-DOWN 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: --.

 */

public class Pattern/**n o w j a v a . c o m - 时  代  Java**/

{

  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 = array.length, y = 0;

    boolean up = true, down = false;


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

    {

      if( up )

      {

        x --;/*from 时 代 J a v a 公 众 号*/

      }

      else

      {

        x ++;

      }


      if( x == - 1 )

      {

        x ++;

        y ++;

        up = false;

        down = true;

      }


      if( x == array.length )

      {

        x --;

        y ++;

        up = true;

        down = false;

      }


      array[ x ][ y ] = k;

    }


    
展开阅读全文