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的矩阵,哪位能帮帮忙?

解决方案 »

  1.   

    int i=0,j=0;
    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!");开头,代码可以这么写,你试试!
      

  2.   

    import java.util.Scanner;public class MatrixTest {
         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();
     }
     
     
         }
    }
      

  3.   

    利用输入流 class FromKeyBoard{
    //初始化用来接收从键盘输入的字符串
    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()
    //几下来就不用说了吧?祝你好运!
      

  4.   

    我是初学者,也试着写一下,编译通过了/*This program tends to transpose the Matrix user input
     *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();
           }
                 
          
       }
    }