Package Demo2;
Import Java.util.Scanner;
public class Main {
private static Scanner input = new Scanner (system.in);
public static void Main (string[] args) {
1: Initialize a matrix first
2: Print the values in this matrix
Printmatrix (init (+));
}
public static int [] [] init (int row,int col) {
int [] [] matrix = new Int[row][col];
for (int i = 0;i<row;i++) {
for (int j = 0;j<col;j++) {
Matrix [I][j] = Input.nextint ();
}
}
return matrix;
}
public static void Printmatrix (int [] [] matrix) {
int tr = 0;
int TC = 0;
int dr = Matrix.length-1;
int dc = matrix[0].length-1;
while (tr <= Dr && tc <= DC) {
Printedge (Matrix,tr++,tc++,dr--, dc--);
}
}
private static void Printedge (int[][] Matrix, int tr, int tc, int dr, int dc) {
1: First judge if this matrix is not only one row of the situation
if (tr = = DR) {
for (int i = Tc;i <= dc;i++) {
System.out.print (Matrix[tr][i] + "");
}
}
2: Judge if this matrix is not only a column of circumstances
else if (tc = = DC) {
for (int i = Tr;i <= dr;i++) {
System.out.print (MATRIX[I][TC] + "");
}
}else{//the general situation
int CURC = TC;
int curr = TR;
while (CURC! = DC) {
System.out.print (MATRIX[TR][CURC]);
curc++;
}
while (curr! = dr) {
System.out.print (MATRIX[CURR][DC]);
curr++;
}
while (CURC! = TR) {
System.out.print (MATRIX[DR][CURC]);
curc--;
}
while (Curr! = TR) {
System.out.print (MATRIX[CURR][TC]);
curr--;
}
}
}
}
Array and matrix problems in the Circle print Group