Java Small Example: output a numeric matrix in a clockwise or counterclockwise direction __java

Source: Internet
Author: User
Tags string format

Title: In accordance with the specified length and output direction, from the outside to print a starting from the 1 digital matrix, the starting position of the matrix in the upper-left corner. The following figure

The code and comments are as follows:

public class Numbermatrix {public static void main (string[] args) {int width = 25;
        int height = 12;

        Boolean clockwise = false;
    Outputmatrix (width, height, clockwise);
     /** * Prints a 1-based numeric matrix in the specified length and output direction, starting at the upper-left corner of the matrix. * * @param width Matrix breadth * @param height matrix altitude * @param clockwise is clockwise/private static VO ID outputmatrix (int width, int height, boolean clockwise) {//First determine the number of digits of the maximum number to determine how the output is aligned to the int numlength = (int

        ) math.log10 (Width * height) + 1;

        Determines the format of the output (maximum number of digits + 1 spaces) String format = "%" + (Numlength + 1) + "D";

        Defines a two-dimensional array to output, note that the dimension is from high to low/the value of all elements in the matrix is 0 int[][] matrix = new Int[height][width];
        Define a position pointer and a counter, position the pointer to move, and the counter is responsible for increments, the increment of the number//is filled into the matrix, when the width of the height of the number filled, the matrix is completed.
        Note that the first element of the position pointer corresponds to the first dimension y of the Matrix, and the second dimension x corresponds to the second element.
        int[] pointer = {0, 0};

        int counter = 1; Define Current moveDirection: 1, 2, 3, 4, respectively, above, right, down, left.
        The clockwise start direction is the right, and the counterclockwise starting direction is below. int direction = clockwise?

        2:3; Start the loop fill, each filled in three steps for (int i = 1, max = width * height; i <= max; i++) {//1. Fill in content I
            NT y = pointer[0];
            int x = pointer[1];

            MATRIX[Y][X] = counter; 2.

            Counter from counter + + 1; 3.
        Move to the next location, because this place is more complex, so open a method to implement direction = Moving (matrix, width, height, pointer, direction, clockwise);  (int y = 0; y < height; y++) {for (int x = 0; x < width;)//matrix fill complete.
            X + +) {System.out.printf (format, matrix[y][x]);  } System.out.println ();
     Output line Break}/** * in Matrix moving * @param matrix matrix to determine whether the next position in the forward direction has been filled with numbers, and if so, turn * The height of the width matrix @param the height matrix of the @param width @param the current position of the pointer pointer. When this method is invoked, the value inside will change unless the method returns 0 * @param directionThe direction in which the pointer is currently moving * @param whether the clockwise is to be rotated in a clockwise direction * @return The new direction after the move (may be the same as the original direction). If no further moves are possible, return 0/private static int move (int[][] matrix, int width, int height, int[] pointer, int direction, b

        Oolean clockwise) {//First try to move in the original direction to Newpointer int[] Newpointer = movedirectly (pointer, direction);
            Check if the newpointer is legitimate, assign it to pointer if it is valid, and keep the original direction by completing the IF (IsValid (Newpointer, matrix, width, height)) {
            System.arraycopy (newpointer, 0, pointer, 0, 2);
        return direction;
        //Make a turn, move back from pointer to new direction direction = Turn (direction, clockwise);

        Newpointer = movedirectly (pointer, direction); Check to see if the newpointer is legal (same as the previous surface) if (IsValid (Newpointer, matrix, width, height)) {system.arraycopy (Newpoi
            nter, 0, pointer, 0, 2);
        return direction;
        ///cannot move forward or turn, then cannot continue moving.
    return 0; }//Judge whether the specified position in the matrix can populate the private static Boolean IsValid (int[] newpointer, int[][] matrix, int width, int height) {//position cannot exceed matrix range if (newpointer[0) >= Height | | Newpointer[0] < 0 | | NEWPOINTER[1] >= Width | |
        Newpointer[1] < 0) {return false;
        The content of//position should be null if (Matrix[newpointer[0]][newpointer[1]]!= 0) {return false;
    return true; }//steering. According to our definition of direction, clockwise is +1, counterclockwise is-1 private static int turn (int direction, boolean clockwise) {int Newdirec tion = clockwise?

        Direction + 1:direction-1;
        if (Newdirection > 4) {newdirection = 1;
        else if (Newdirection < 1) {newdirection = 4;
    return newdirection; /** * Moves in the specified direction and returns a new position * * @param pointer Current position * @param direction Direction * * @return New Position */private static int[] movedirectly (int[] pointer, int direction) {int y = pointer[0];

        int x = pointer[1];
            switch (direction) {case 1:return new int[]{y-1, x};
            Case 2:return New Int[]{y, x + 1};
            Case 3:return New Int[]{y + 1, x};
        Case 4:return new Int[]{y, x-1};
    } throw new IllegalArgumentException ("Incorrect direction:" + direction); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.