0

我有一个对角矩阵,我正在尝试为矩阵的总和编写一个函数。

这是我到目前为止所拥有的:

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”

谁能帮忙?

4

1 回答 1

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

对角线元素的总和是:22.0 和 5.0

于 2013-11-12T19:19:46.780 回答