Joke Collection Website - Joke collection - JAVA program: Input an M*N matrix from the console, and output the matrix after exchanging rows and columns.

JAVA program: Input an M*N matrix from the console, and output the matrix after exchanging rows and columns.

//Here is the code I wrote. Although a little too much, I may play a joke on the master, but I have been thinking about it for more than an hour. I hope LZ will give points (it will not be effective until I have tested it).

Import java.io.bufferedreader;

Import java.io.ioexception;

Import java.io.inputstreamreader;

Import java.util.scanner;

Public course teacher 1 {

//Input an M*N matrix from the console, and output the matrix after exchanging rows and columns.

Public static void main(String[] args){

Scanner sc = new scanner (system. in);

System.out.println ("Please enter m ...");

int m = sc . nextint();

System.out.println ("Please enter m ...");

int n = sc . nextint();

System.out.println ("The m and n you entered are" +m+ "and"+n "respectively);

Boolean allow = false

Do {

allow = false

System.out.println ("Now you need to enter" +m*n+ "integers continuously, separated by commas \ n");

buffered reader br = new buffered reader(new InputStreamReader(system . in));

String line = null

Try {

line = br . readline();

} catch (IOException e) {

e . printstacktrace();

}

String [] s = line.trim()。 Split (",");

if(s.length! = m*n){

System.out.println ("The input length is wrong, please re-enter!" );

allow = true

} Otherwise {

String [][] str2s = new string [m] [n];

int count = 0;

for(int I = 0; I & ltm;; i++){

for(int j = 0; j & ltn; j++){

str2s[I][j]= s[count++];

}

}

System. Out. println("- as-is array-");

print2Array(str2s,m,n);

System. Out. println("- replace result-");

replace2Array(str2s,m,n);

}

}while (allow);

}

private static void print 2 array(String[][]str2s,int m,int n){

for(int I = 0; I & ltm;; i++){

for(int j = 0; j & ltn; j++){

system . out . print("+str2s[I][j]+" ");

}

system . out . print(" \ n ");

}

}

private static void replace 2 array(String[][]str2s,int m,int n){

String line[] = new string [m * n];

int count = 0;

for(int I = 0; I & ltn;; i++){

for(int j = 0; j & ltm; j++){

line[count++]= str2s[j][I];

}

}

count = 0;

String[][] strR2s = new string [n] [m];

for(int I = 0; I & ltn;; i++){

for(int j = 0; j & ltm; j++){

str r2s[I][j]= line[count++];

}

}

print2Array(strR2s,n,m);

}

}