编写一个 Java 程序来执行标量矩阵乘法,并附带示例。或者编写一个 Java 程序来计算给定多维数组的标量乘法。
在此标量矩阵乘法示例中,我们声明了一个 3*3 的 Sc_Mat 整型矩阵。接下来,我们允许用户输入任何整数值来执行标量乘法。然后,我们使用 for 循环来迭代矩阵,并在其中执行乘法。
import java.util.Scanner;
public class ScalarMultiplication {
private static Scanner sc;
public static void main(String[] args) {
int[][] Sc_Mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
sc= new Scanner(System.in);
int i, j, num;
System.out.println("\nPlease Enter the Multiplication Value : ");
num = sc.nextInt();
System.out.println("\nMatrix after Scalar Multiplication are :");
for(i = 0; i < Sc_Mat.length ; i++)
{
for(j = 0; j < Sc_Mat[0].length; j++)
{
System.out.format("%d \t", (num * Sc_Mat[i][j]));
}
System.out.print("\n");
}
}
}

Java 标量矩阵乘法程序示例 2
此 Java 标量矩阵乘法代码与上述相同。但是,此标量矩阵 代码允许用户输入行数、列数和 矩阵项。请参考 C 语言标量矩阵乘法程序文章来理解分步循环执行。
import java.util.Scanner;
public class ScalarMultiplication {
private static Scanner sc;
public static void main(String[] args) {
sc= new Scanner(System.in);
int i, j, rows, columns, num;
System.out.println("\n Enter Scalar Matrix Rows & Columns : ");
rows = sc.nextInt();
columns = sc.nextInt();
int[][] Sc_Mat = new int[rows][columns];
System.out.println("\n Enter the First Matrix Items : ");
for(i = 0; i < rows; i++) {
for(j = 0; j < columns; j++) {
Sc_Mat[i][j] = sc.nextInt();
}
}
System.out.println("\nPlease Enter the Multiplication Value : ");
num = sc.nextInt();
System.out.println("\nMatrix after Scalar Multiplication are :");
for(i = 0; i < Sc_Mat.length ; i++)
{
for(j = 0; j < Sc_Mat[0].length; j++)
{
System.out.format("%d \t", (num * Sc_Mat[i][j]));
}
System.out.print("\n");
}
}
}
Enter Scalar Matrix Rows & Columns :
3 3
Enter the First Matrix Items :
10 20 30
40 60 80
90 11 55
Please Enter the Multiplication Value :
4
Matrix after Scalar Multiplication are :
40 80 120
160 240 320
360 44 220