Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对二维数组求和的方法_Java_Arrays_For Loop - Fatal编程技术网

Java 对二维数组求和的方法

Java 对二维数组求和的方法,java,arrays,for-loop,Java,Arrays,For Loop,我在数组一章学习Java和Im。活动是,我需要编写一个方法来接收二维double数组,并求这两个数组的和。到目前为止,我写了以下内容: public class arraysExercise{ public static double suma(double[] arreglo){ double total = 0.0; for(int i=0;i<arreglo.length;i++){ total+=arreglo[i]; } ret

我在数组一章学习Java和Im。活动是,我需要编写一个方法来接收二维double数组,并求这两个数组的和。到目前为止,我写了以下内容:

public class arraysExercise{


public static double suma(double[] arreglo){
    double total = 0.0;
    for(int i=0;i<arreglo.length;i++){
        total+=arreglo[i];
    }
    return total;
}

public static double sum(double[][] arreglo){
    double total = 0.0;
    for(int i=0;i<arreglo.length;i++){
        for(int j=0;j<arreglo[i].length;j++){
            total+=arreglo[i][j];
        }
    }
    return total;
}

//Arreglo que promedia valores double
public static double promedio(double[] arreglo){
    double total=suma(arreglo);
    return total/arreglo.length;
}


/*public static double mode(double[] arreglo){
    double maximo = 0;
    double repetido = 0;

    for(int i=0;i<arreglo.length;i++){
        count(arreglo[i])++;

        if(repetido < count(arreglo[i])){
            repetido = count(arreglo[i]);
            maximo = arreglo[i];
        }

        }
        return maximo;
    }*/


public static void main(String[] args) {
    double[][] valores = ({10.0,10.0,2.5},{10.0,2.0});

    double sum = sum(valores);
    System.out.println(sum);

    /*double promedio = promedio(valores);
    System.out.println("El promedio es: "+promedio);*/

    /*double mode = mode(valores);
    System.out.println("El valor mas comun es: "+mode);*/
}
公共类数组执行{
公共静态双suma(双[]arreglo){
双倍合计=0.0;

对于(inti=0;i这只是一个简单的语法错误;您使用了括号而不是花括号

double[][] valores = ({10.0,10.0,2.5},{10.0,2.0});
需要:

double[][] valores = {{10.0,10.0,2.5},{10.0,2.0}};

这只是一个简单的语法错误;您使用了括号而不是花括号

double[][] valores = ({10.0,10.0,2.5},{10.0,2.0});
需要:

double[][] valores = {{10.0,10.0,2.5},{10.0,2.0}};

您声明的数组不正确。请将第一行中的替换为{
double[][]valores={{10.0,10.0,2.5},{10.0,2.0};

您声明的数组不正确。请将第一行中的替换为{
double[]valores={{10.0,10.0,2.5},{10.0,2.0};

这是您试图运行的代码的精确副本吗?
表达式的非法开始通常意味着缺少
}
{/code>,
,等等。您能告诉我们发生错误的行吗?如图所示的代码中,
main()
方法缺少关闭
}
。请按照您的方式发布整个类。这是您试图运行的代码的精确副本吗?
表达式的非法开始
通常意味着缺少
}
{
等…您能告诉我们发生错误的行吗?在所示的代码中,
main()
方法缺少关闭
}
。请按照您的方式发布整个类。