import java.util.Scanner;
public class Juzheng1
{ public static void main(String args[])
{ int a[][]=new int[3][4]; //预先定义输入3行4列
int b[][]=new int[4][3];
int i,j;
System.out.println("input matrix a:");
for(i=0;i<3;i++)
{ for(j=0;j<4;j++)
a[i][j]=input.nextInt();
}
。。
想写个矩阵转置,可以随便输入m*n的矩阵,然后将其转置并输出,上面写的只是预先定义的几行几列而并不适用与m*n的矩阵,哪位能帮帮忙?
public class Juzheng1
{ public static void main(String args[])
{ int a[][]=new int[3][4]; //预先定义输入3行4列
int b[][]=new int[4][3];
int i,j;
System.out.println("input matrix a:");
for(i=0;i<3;i++)
{ for(j=0;j<4;j++)
a[i][j]=input.nextInt();
}
。。
想写个矩阵转置,可以随便输入m*n的矩阵,然后将其转置并输出,上面写的只是预先定义的几行几列而并不适用与m*n的矩阵,哪位能帮帮忙?
解决方案 »
- 如何把Iterator<Integer>转换为int型
- 关于在myeclipse查看开源源代码的问题
- 想学JBoss,请问在www.jboss.org上下载jboss web native connectors还是jboss web packages?
- 有没有比较好的,能协助JAVA开发WINDOWS系统功能的第三方包
- 100分求java JNI的一个问题
- 求用java编的ftp客户端的原代码
- 编码转换的超难问题?
- 深夜了,有个问题无法解决,无法入睡!!
- 请教一个问题,请大家帮帮我~~
- 取日期的年份的小问题
- 在Appl用里面用什么命令可以关闭Applet?
- 有一个菜鸟,连数据库连不上,请问是什么原因?
if(args.length==2)
{i=Integer.parseInt(args[0]);
j=Integer.parseInt(args[1]);
}
else System.out.println("please enter two number:");
try{
i=System.in.read();
j=System.in.read();
}catch(IOException e)
{e.printStackTrace();}
int a[][]=new int[i][j];
System.out.print("successfull!");开头,代码可以这么写,你试试!
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int row,col;
//输入行
System.out.println("row:");
row = in.nextInt();
//输入列
System.out.println("col:");
col = in.nextInt();
//根据行和列定义存放原矩阵的数组
int[][] originalMatrix = new int[row][col];
//定义存放转置矩形的数组
int[][] transposeMatrix = new int[col][row];
//提示用户输入原矩阵的每个元素,用0为起始坐标
for(int i = 0;i < row;i++){
for(int j = 0; j < col;j++){
System.out.println("original[" + i + "][" + j + "]:");
originalMatrix[i][j] = in.nextInt();
transposeMatrix[j][i] = originalMatrix[i][j];
}
}
//输出原矩阵
System.out.println("originalMatrix:");
for(int i = 0;i < row;i++){
for(int j = 0; j < col;j++){
System.out.printf("%4d",originalMatrix[i][j]);
}
System.out.println();
}
//输出转置后的矩阵
System.out.println("transposeMatrix:");
for(int j = 0;j < col;j++){
for(int i = 0;i < row;i++){
System.out.printf("%4d",transposeMatrix[j][i]);
}
System.out.println();
}
}
}
//初始化用来接收从键盘输入的字符串
String str = null;
//初始化一个将字符串转化成整形的整形变量
int result = 0;
public FromKeyBoard_method(){
System.out.println("输入数值:");
//因为涉及到键盘输入,所以要捕捉异常
try{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
str = bf.readLine();
result = Integer.parseInt(str);
}catch(IOException e){}
rerurn result;
}
//然后在main函数中定义一个FromKeyBoard的对象然后两次调用FromKeyBoard_method()
//几下来就不用说了吧?祝你好运!
*and then outputs the result.
*
*
*/import java.util.Scanner;
import java.io.IOException;public class TranMatrix{
public static void main(String[] args){
//To get rows and columns Matrix
int row,col;
Scanner scanner1= new Scanner(System.in);
System.out.println("please enter two number:");
row=scanner1.nextInt();
col=scanner1.nextInt();
scanner1=null;
int a[][]=new int[row][col];
System.out.println("successfull!");
//To input the source Matrix
System.out.println("Rows = "+row+" Columns ="+col);
System.out.println("Please input matrix a:");
Scanner scanner= new Scanner(System.in);
for(int i=0;i<row;i++)
{ for(int j=0;j<col;j++)
{a[i][j]=scanner.nextInt();}
}
//To output the Matrix user input
System.out.println(" Matrix a:");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{System.out.print(a[i][j]);
System.out.print(" ");
}
System.out.println();
}
// To transpose the source Matrix
int b[][]=new int[row][col];
for(int i=0;i<row;i++)
{ for(int j=0;j<col;j++)
{
b[i][j]=a[i][j];}
}
//To output the Transpose Matrix
System.out.println(" Matrix b=aT follows as:");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{System.out.print(b[j][i]);
System.out.print(" ");
}
System.out.println();
}
}
}