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.
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);
}
}
- Previous article:A collection of super funny jokes that will last forever
- Next article:Fan xian funny video joke
- Related articles
- Recorded information is always exposed on the Internet. Is caution important?
- Children should hold the doll when they sleep, and cry if they don't. How to correct his bad habits?
- Why do you call your father-in-law "Old Mount Tai"? Why is it called "grandfather" again?
- Eat, sleep and beat peas. What does pea mean?
- An absurd example of literary inquisition
- Mothers are arrogant to their daughters, often unreasonable, and polite to their daughters-in-law. Why?
- What is the personality of a woman with a sharp chin? Can you make friends with her?
- What are the stories about the origin of the Year of the Tiger?
- An executive was reported by his wife's real name to a derailed female employee, using power for personal gain. Is the report true?
- Quarrel with the cold war mood and say a word about the collection of WeChat friends circle.