矩阵加减乘法的实现

## 矩阵运算!
![矩阵如下](https://img-blog.csdnimg.cn/0a32201962b14c239eb75a7f1c831d35.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2MDg2MjIz,size_16,color_FFFFFF,t_70#pic_center)
![矩阵相乘结果](https://img-blog.csdnimg.cn/459da57bff924beba2838699c0fafd29.png#pic_center)

package com.company;


import java.util.Scanner;

public class Matrix {

    public static int nums[][] = new int[3][3];
    public static int nums1[][] = new int[3][3];
    public static int tempone[][] = new int[3][3];
    public static int temptwo[][] = new int[3][3];
    public static int temp1[][] = new int[3][3];
    public static int temp2[][] = new int[3][3];
    public static int result[][] = {{0,0,0},{0,0,0},{0,0,0}};
    public static int num=0;


    //写的输入函数有问题,矩阵的值会被覆盖;
    //在做矩阵乘法是出现了问题;因为用了临时矩阵来作为存储值得容器;
    public Matrix(){
        num++;

    };
    public static int[][] inputLeft() {
        Scanner src = new Scanner(System.in);

        System.out.println("请用户输入一个二维矩阵:");
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                nums[i][j] = src.nextInt();
            }
            System.out.println("用户结束" + (i + 1) + "输入");
        }
        System.out.println("二维矩阵如下:");
        for (int x = 0; x < nums.length; x++) {
            for (int y = 0; y < nums.length; y++) {
                System.out.print(nums[x][y] + "\t");
            }
            System.out.println();
        }



        tempone = nums;
        return tempone;

    }
    public static int [][] inputRight(){
        Scanner src1 = new Scanner(System.in);
        int result1[][]=new int[3][3];
        System.out.println("请输入一个二维矩阵:");
        for(int i=0;i<nums1.length;i++){
            for(int j=0;j<nums1.length;j++){
              nums1[i][j]=src1.nextInt();
            }
            System.out.println("用户结束" + (i + 1) + "输入");
        }
        System.out.println("二维矩阵如下:");
        for (int x = 0; x < nums1.length; x++) {
            for (int y = 0; y < nums1.length; y++) {
                System.out.print(nums1[x][y] + "\t");
            }
            System.out.println();
        }


        result1=nums1;
        return result1;
    }
//    遍历输入的矩阵,看输入的值是否赋值成功;
//    public static void lOOKtempMatrix(){
//        System.out.println("遍历一个临时矩阵:");
//        for(int w=0;w<temp.length;w++){
//            for(int u=0;u<temp.length;u++){
//                System.out.println(temp[w][u]);
//            }
//            }
//
//    }

    public static void addMatrix() {
        temp1 = inputLeft();
        temp2 = inputRight();
        System.out.println("矩阵加法如下:");
        for (int i = 0; i < temp1.length; i++) {
            for (int j = 0; j < temp1[0].length; j++) {
                temp2[i][j] += temp1[i][j];
                System.out.print(temp2[i][j] + "\t");

            }
            System.out.println();
        }


    }

    public static void subMatrix() {
        temp1 = inputLeft();

        temp2 = inputRight();
        for (int i = 0; i < temp1.length; i++) {
            for (int j = 0; j < temp1[0].length; j++) {
                temp1[i][j] -= temp2[i][j];
                System.out.println(temp1[i][j] + "\t");
            }
            System.out.println();
        }
    }

    public static int [][] multipleMatrix() {

//        int temp1[][]={{1,2,3},{4,5,6},{7,8,9}};
//        int temp2[][]={{1,0,0},{0,1,0},{0,0,1}};
          int temp1[][]=inputLeft();
          int temp2[][]=inputRight();

        System.out.println("使用矩阵乘法!");



        for (int i = 0; i < temp1.length; i++) {
            for (int j = 0; j < temp1.length; j++) {


                for (int s = 0; s < temp1.length; s++) {
                    result[i][j] += temp1[i][s] * temp2[s][j];
                }

            }

        }
//      for(int i=0;i<result.length;i++){
//          for(int j=0;j<temp[0].length;j++){
//              System.out.print(temp[i][j]+"\t");
//          }
//          System.out.println();
//      }

//       return temp;
        return result;
    }

   public static void multiple(){
       int temp3[][]=new int[3][3];

       temp3=multipleMatrix();
       for(int i=0;i<temp3.length;i++){
           for(int j=0;j<temp3[0].length;j++){
               System.out.print(temp3[i][j]+"\t");
           }
           System.out.println();
       }
   }






    public static void main(String args[]){
//        Scanner src1=new Scanner(System.in);
//        String a="加法";
//        String b="b";
//        String c="c";
//        String d="d";
//        System.out.println("请先选择你要进行那类计算:"+"a加法\t"+"b减法\t"+"c乘法\t"+"d除法");

//       String result= src1.nextLine();
//       System.out.println(a+result);
//       if(a==result){
//           System.out.println("你选择了加法");
//          addMatrix();
//       }
//       else if(b==result){
//           System.out.println("你选择了减法");
//           subMatrix();
//       }
//       else if(c==result){
//           System.out.println("你选择了除法");
//           multipleMatrix();
//       }
//       else if(d==result){
//           System.out.println("除法");
//       }
//       else{
//           System.out.println("程序错误!");
//       }

//       接下来通过多分支条件语句来控制选择那种方法;并且优化矩阵乘法;减小时间复杂度;加上矩阵秩的运算。优化成图形操作界面;此程序就算完成。

//        addMatrix();//矩阵加法;

//        subMatrix();//矩阵减法;

        multiple();//矩阵乘法;



    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AimerGosick

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值