Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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_Multidimensional Array - Fatal编程技术网

Java 如何在二维数组中找到最大行

Java 如何在二维数组中找到最大行,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,创建二维阵列,例如m: 要求用户从键盘输入行和列的大小(使用扫描仪)。N为9,如果用户输入的列大小大于N+4,请用户重新输入列大小 使用随机对象将所有数组元素填充为(3.0,13.0)范围内的双倍数 传递上面的数组m并调用以下两个方法 在findMaxRow(双[][]数组)中,查找并打印二维数组中最大的列和 在returnAvg(m)中,打印出数组m的平均值 评论: 所以,我编写了查找max Colm的代码,但我不知道如何查找max行。我需要能够找到最大行,但我正在考虑如何找到,因为我的代

创建二维阵列,例如m:

  • 要求用户从键盘输入行和列的大小(使用扫描仪)。N为9,如果用户输入的列大小大于N+4,请用户重新输入列大小
  • 使用随机对象将所有数组元素填充为(3.0,13.0)范围内的双倍数
  • 传递上面的数组m并调用以下两个方法

    • findMaxRow(双[][]数组)
      中,查找并打印二维数组中最大的列和
    • returnAvg(m)
      中,打印出数组
      m的平均值
  • 评论:

    所以,我编写了查找max Colm的代码,但我不知道如何查找max行。我需要能够找到最大行,但我正在考虑如何找到,因为我的代码找到的是cllm而不是最大行

    下面是我的代码:import java.util.Scanner

      public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Random rand = new Random();
    
        System.out.print("Number of Rows? ");
        int rows = input.nextInt();
        System.out.print("Number of Columns? ");
        int columns = input.nextInt();
        while (columns > 7) { // check for column > n+5 where n = 2 in my case
            System.out.print("The column amount is too high. Try another number: ");
            columns = input.nextInt();
        }
    
        double[][] m = new double[rows][columns];
    
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                m[i][j] = rand.nextDouble()*7+4;
            }
        }
        findMaxCol(m);
        System.out.println("The average value of this array is "+returnAvg(m));
    }
    public static void findMaxCol(double[][] a) {
        double[] maxCol = new double[a[0].length];
        double max = 0;
        int maxColNum=0;
        for (int i = 0; i < a[0].length; i++) { // Sum of Columns
            for (int j = 0; j < a.length; j++) {
                maxCol[i]+=a[j][i];
            }
        }
        //System.out.println(Arrays.toString(maxCol));
        for (int i = 0; i < maxCol.length; i++) {
            if (max < maxCol[i]) {
                max = maxCol[i];
                maxColNum = i; // column number will associate with its array column starting with 0
            }
        }
        System.out.println("The highest column sum is Column #"+maxColNum+" with a sum of "+max);
    }
    public static double returnAvg(double[][] a) {
        double sum = 0; // initialization
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                sum+=a[i][j];
            }
        }
        // System.out.println(sum); Test line
        return (sum/(a.length*a[0].length)); // avg
    }
    
    publicstaticvoidmain(字符串[]args){
    扫描仪输入=新扫描仪(System.in);
    Random rand=新的Random();
    系统输出打印(“行数?”);
    int rows=input.nextInt();
    系统输出打印(“列数?”);
    int columns=input.nextInt();
    while(columns>7){//检查column>n+5,在我的例子中n=2
    System.out.print(“列数量太高,请尝试其他数字:”);
    columns=input.nextInt();
    }
    double[]m=新的双[行][列];
    对于(int i=0;i

    }我不确定我是否理解您想要的结果,但这应该会给您一行最大的总和,以及该总和是多少。注意,这不考虑具有相等和的多行。它将报告数组中具有最大和的第一行

    public static void findMaxRow(double[][] a){
    
        double maxSum = 0;
        int maxRow = 0;
    
        for (int row = 0; row < a.length; row++){
    
            double rowSum = 0;
    
            for (int column = 0; column < a[row].length; column++){
                rowSum += a[row][column];
            }
    
            if (rowSum > maxSum){
                maxSum = rowSum;
                maxRow = row;
            }
        }
    
        // maxSum should be the greatest sum of any row
        // maxRow is the row that contained the greatest sum
    }
    
    publicstaticvoidfindmaxrow(double[]a){
    双最大和=0;
    int maxRow=0;
    for(int row=0;row最大和){
    最大和=行和;
    maxRow=行;
    }
    }
    //maxSum应该是任何行中的最大和
    //maxRow是包含最大和的行
    }
    
    列和行只是我们用来建模数据的很好的抽象;它们本身并不是2D阵列的固有方面。你能澄清你到底在问什么并把你的代码示例压缩成一个吗?我的代码只找到max列,我希望它找到行。我怎样才能做到这一点。我是java新手,需要帮助。你没有考虑我刚才说的任何内容。你能详细说明一下吗?这就是我需要它做的事情。如果数组都是负数怎么办?rowSum的值永远不会大于0,因此答案将始终保持为0。我被困在那里了。你能想办法解决这个问题吗?