JAVA程序:从控制台输入一个M*N矩阵,输出行列互换后的矩阵

2024-11-26 10:37:50
推荐回答(3个)
回答1:

//以下是我写的代码,虽然有点儿多,给高手看了可能还惹笑话,但好歹也花了我一个多小时琢磨,希望LZ给分 (本人测试后有效)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Teacher1 {
//从控制台输入一个M*N矩阵,输出行列互换后的矩阵
public static void main(String[] args){

Scanner sc = new Scanner(System.in);
System.out.println("请输入M...");
int m = sc.nextInt();
System.out.println("请输入M...");
int n = sc.nextInt();

System.out.println("你输入的M和N分别为:"+m+" , "+n);

boolean allow = false;
do{
allow = false;
System.out.println("现在您需要连续输入 "+m*n+"个整数,并以逗号隔开\n");
BufferedReader br = new BufferedReader(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("输入长度有误,请重输!");
allow = true;
}else{
String [][] str2s = new String[m][n];
int count = 0;
for(int i=0;i for(int j=0;j str2s[i][j] = s[count++];
}
}

System.out.println("----数组原样----");
print2Array(str2s, m, n);
System.out.println("----替换结果----");
replace2Array(str2s, m, n);
}
}while(allow);
}

private static void print2Array(String[][] str2s,int m, int n){
for(int i=0;i for(int j=0;j System.out.print(" "+str2s[i][j]+" ");
}
System.out.print("\n");
}
}

private static void replace2Array(String[][] str2s, int m, int n){
String line[] = new String[m*n];
int count = 0;
for(int i=0;i for(int j=0;j line[count++] = str2s[j][i];
}
}

count = 0;
String[][] strR2s = new String[n][m];
for(int i=0;i for(int j=0;j strR2s[i][j] = line[count++];
}
}
print2Array(strR2s, n ,m);
}

}

回答2:

撸过 代码自己写 不要复制

回答3:

稍等一会发给你