使用 for 循环编写一个 Java 程序来查找矩阵的范数。矩阵的范数是矩阵项平方和的平方根。在此范例中,我们使用嵌套的 for 循环来迭代矩阵并查找矩阵的平方。接下来,Math.sqrt 函数将找到矩阵的平方根。
package NumPrograms;
import java.util.Scanner;
public class MatrixNormal1 {
private static Scanner sc;
public static void main(String[] args) {
int i, j, rows, columns, normal = 0;
sc= new Scanner(System.in);
System.out.print("Enter Normal Matrix Rows and Columns = ");
rows = sc.nextInt();
columns = sc.nextInt();
int[][] Norm_arr = new int[rows][columns];
System.out.println("Please Enter the Normal Matrix Items = ");
for(i = 0; i < rows; i++)
{
for(j = 0; j < columns; j++)
{
Norm_arr[i][j] = sc.nextInt();
}
}
for(i = 0; i < rows; i++)
{
for(j = 0; j < columns; j++)
{
normal = normal + (Norm_arr[i][j]) * (Norm_arr[i][j]);
}
}
double actNormal = Math.sqrt(normal);
System.out.println("\nThe Square Of the Matrix = " + normal);
System.out.println("The Normal Of the Matrix = " + actNormal);
}
}

使用 while 循环查找矩阵范数的 Java 程序
package NumPrograms;
import java.util.Scanner;
public class MatrixNormal2 {
private static Scanner sc;
public static void main(String[] args) {
int i, j, rows, columns, normal = 0;
sc= new Scanner(System.in);
System.out.print("Enter Normal Matrix Rows and Columns = ");
rows = sc.nextInt();
columns = sc.nextInt();
int[][] Norm_arr = new int[rows][columns];
System.out.println("Please Enter the Normal Matrix Items = ");
i = 0;
while(i < rows)
{
j = 0;
while(j < columns)
{
Norm_arr[i][j] = sc.nextInt();
normal = normal + (Norm_arr[i][j]) * (Norm_arr[i][j]);
j++;
}
i++;
}
double actNormal = Math.sqrt(normal);
System.out.println("\nThe Square Of the Matrix = " + normal);
System.out.println("The Normal Of the Matrix = " + actNormal);
}
}
Enter Normal Matrix Rows and Columns = 3 3
Please Enter the Normal Matrix Items =
15 25 35
45 55 65
75 85 95
The Square Of the Matrix = 33225
The Normal Of the Matrix = 182.27726133558184
此 示例 使用 do while 循环来计算并打印给定矩阵的范数。
package NumPrograms;
import java.util.Scanner;
public class MatrixNormal3 {
private static Scanner sc;
public static void main(String[] args) {
int i, j, rows, columns, normal = 0;
sc= new Scanner(System.in);
System.out.print("Enter Normal Matrix Rows and Columns = ");
rows = sc.nextInt();
columns = sc.nextInt();
int[][] Norm_arr = new int[rows][columns];
System.out.println("Please Enter the Normal Matrix Items = ");
i = 0;
do
{
j = 0;
do
{
Norm_arr[i][j] = sc.nextInt();
normal = normal + (Norm_arr[i][j]) * (Norm_arr[i][j]);
}while(++j < columns);
} while(++i < rows);
double actNormal = Math.sqrt(normal);
System.out.println("\nThe Square Of the Matrix = " + normal);
System.out.println("The Normal Of the Matrix = " + actNormal);
}
}
Enter Normal Matrix Rows and Columns = 3 3
Please Enter the Normal Matrix Items =
10 20 30
40 50 60
70 80 90
The Square Of the Matrix = 28500
The Normal Of the Matrix = 168.81943016134133