java学习day8-day10

107 阅读2分钟

day8 矩阵相乘

8.1 题目解读

矩阵相乘(只有第一个矩阵的列和第二个矩阵的行相等): a矩阵(mn),b矩阵(np)则能相乘,且相乘后的矩阵为m*p。故在矩阵相乘是在一定条件下才能进行,需要用到if判断。 在这里插入图片描述

8.2代码

package basic;

import java.util.Arrays;

public class MatrixMultiplication {
    public static void main(String[] args) {
        matrixMultiplicationTest();
    }

    /**
     * Matrix multiplication. The columns of the first matrix should be equal to the rows of the second one.
     * @param paraFirstMatrix The first matrix.
     * @param paraSecondMatrix The second matrix
     * @return The result matrix.
     */
    public static int[][] multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix){
        //m*n n*p == m*p
        int m = paraFirstMatrix.length;
        int n = paraFirstMatrix[0].length;
        int p = paraSecondMatrix[0].length;

        // Step 1. Dimension check.
        if (paraSecondMatrix.length != n) {
            System.out.println("The two matrices cannot be multiplied.");
            return null;
        }

        // Step 2. The loop. m*n n*p == m*p
        int[][] resultMatrix = new int[m][p];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < p; j++) {
                for (int k = 0; k < n; k++) {
                    resultMatrix[i][j] += paraFirstMatrix[i][k] * paraSecondMatrix[k][j];
                }
            }
        }

        return resultMatrix;
    }

    public static void  matrixMultiplicationTest(){
        int[][] tempFirstMatrix = new int[2][3];
        for (int i = 0; i < tempFirstMatrix.length; i++) {
            for (int j = 0; j < tempFirstMatrix[0].length; j++) {
                tempFirstMatrix[i][j] = i + j;
            }
        }
        System.out.println("The first matrix is: \r\n" + Arrays.deepToString(tempFirstMatrix));

        int[][] tempSecondMatrix = new int[3][2];
        for (int i = 0; i < tempSecondMatrix.length; i++) {
            for (int j = 0; j < tempSecondMatrix[0].length; j++) {
                tempSecondMatrix[i][j] = i * 10 + j;
            }
        }
        System.out.println("The second matrix is: \r\n" + Arrays.deepToString(tempSecondMatrix));

        int[][] tempThirdMatrix = multiplication(tempFirstMatrix, tempSecondMatrix);
        System.out.println("The third matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));

        System.out.println("Trying to multiply the first matrix with itself.\r\n");
        tempThirdMatrix = multiplication(tempFirstMatrix, tempFirstMatrix);
        System.out.println("The result matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
    }
}


在这里插入图片描述

day9 while 语句

代码

还有一种循环是 do...while,其循环至少要执行一次循环体,而for和while循环需要先判断条件是否成立 在决定是否执行循环语句

package basic;

public class WhileStatement {
    public static void main(String[] args) {
        whileStatementTest();
    }

    /**
     * The sum not exceeding a given value.
     */
    public static void whileStatementTest() {
        int tempMax = 100;
        int tempValue = 0;
        int tempSum = 0;

        // Approach 1.
        while (tempSum <= tempMax) {
            tempValue++;
            tempSum += tempValue;
            System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);
        }
        tempSum -= tempValue;

        System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);

        // Approach 2.
        System.out.println("\r\nAlternative approach.");
        tempValue = 0;
        tempSum = 0;
        while (true) {
            tempValue++;
            tempSum += tempValue;
            System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);

            if (tempMax < tempSum) {
                break;
            }
        }
        tempSum -= tempValue;

        System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);
    }
}

在这里插入图片描述 在这里插入图片描述