我有一个对角矩阵,我正在尝试为矩阵之和写一个函数。
这是我到目前为止所做的:
import java.io.*;
public class DiagonalMatrices
{
public static void main(String[] args) throws IOException
{
double matrix[][] = {
{10,0,0,0,0},
{0,10,0,0,0},
{0,0,5,0,0},
{0,0,0,-10,0},
{0,0,0,0,7}
};
int j = 0, k = 0;
double sum1 = 0, sum2 = 0;
for ( j = 0; j < matrix.length; j++)
{
for (k = 0; k < matrix.length; k++)
System.out.print(matrix[j][k] + " ");
System.out.println();
}
for ( j = 0; j < matrix.length; j++)
{
sum1 = sum1 + matrix[j][j];
}
k = matrix.length - 1;
for ( j = 0; j < matrix.length; j++)
{
if (k >= 0)
{
sum2 = sum2 + matrix[j][k];
k--;
}
}
System.out.println("Sum of Digonal elements are :" + sum1 + " and " + sum2);
}
}`
我得到100个错误,都说......
“非法字符:\ 0”
任何人都可以帮忙吗?
答案 0 :(得分:2)
你的代码的问题是`在源文件的末尾:)。
删除它,并观察你的输出:
10.0 0.0 0.0 0.0 0.0
0.0 10.0 0.0 0.0 0.0
0.0 0.0 5.0 0.0 0.0
0.0 0.0 0.0 -10.0 0.0
0.0 0.0 0.0 0.0 7.0
Digonal元素的总和是:22.0和5.0