简体   繁体   English

如何编写一个程序,返回二维数组指定列中所有元素的总和

[英]How to write a program that returns the sum of all elements in a specified column of a two-dimensional array

Any help would really be appreciated, majorly stuck at this problem it simply wont run i am at a lost point, the criteria is :任何帮助都将不胜感激,主要是停留在这个问题上,它根本无法运行我处于迷茫状态,标准是:

Write a program that returns the sum of all elements in a specified column of a two-dimensional array.编写一个程序,返回二维数组指定列中所有元素的总和。

Firstly ask the user to enter a 3 by 4 array.首先要求用户输入一个 3 x 4 的数组。 The user should enter the array as follows: 2.6 5.1 6 8 5.4 4.4 7 1 9.5 7.9 2 3用户应按如下方式输入数组: 2.6 5.1 6 8 5.4 4.4 7 1 9.5 7.9 2 3

The program should then calculate the sum of each column in the array.然后程序应该计算数组中每一列的总和。

Tried casting to double or ints nothing works尝试铸造双倍或整数没有任何作用

package com.company;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        double[] Array1 = new double[5];
        double[] Array2 = new double[5];
        boolean Equal = true;

        Scanner input = new Scanner(System.in);
        System.out.print("Please enter " + Array1.length + " values");
        for (int i = 0; i < Array1.length; i++) {
            Array1[i] = input.nextDouble();
        }
        Scanner input2 = new Scanner(System.in);
        System.out.print("Please enter " + Array2.length + " values for your second array:");
        for (int i = 0; i < Array2.length; i++)
            Array2[i] = input.nextDouble();



        if(Array1.length == Array2.length)
        {
            for (int i = 0; i < Array1.length; i++)
            {
                if(Array1[i] != Array2[i])
                {
                    Equal = false;
                }
            }
        }
        else
        {
            Equal = false;
        }

        if (Equal)
        {
            System.out.println("Two Arrays Are Equal");
        }
        else
        {
            System.out.println("Two Arrays Are Not equal");
        }
    }
}
public class Main{

    static double[][] mat;

    public static void main(String[] args){
           Scanner input=new Scanner(System.in);
           System.out.println("Enter number of rows and number of columns");

           int n=input.nextInt();
           int m=input.nextInt();
           mat=new double[n][m];
           System.out.println("Please enter elements of matrix");

           for(int i=0;i<n;i++){
               for(int i=0;i<m;i++){
                   mat[i][j]=input.nextDouble();
               }
           }

          System.out.println("Enter column number to get sum");
          double sum=0D;
          int col=input.nextInt();
          for(int i=0;i<m;i++){
              sum+=mat[i][col];
          }

         System.out.println("sum of elements of "+col+"in the mat is "+sum);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编写一个名为 arraySum 的 function,它返回 int 值的二维数组中所有值的总和(作为 int)(Java) - Write a function called arraySum that returns the sum (as an int) of all the values in a two-dimensional array of int values (Java) 如何获取二维数组中每行最后一列元素的总和? - How to get the sum of the elements in the last column of each row in a two-dimensional array? 拆分指定长度的二维数组 - Split a two-dimensional array of specified length 如何检查数组是否是二维数组中的元素之一 - How to check if an array is one of the elements in a two-dimensional array 如何将所有数组置换迭代地返回到二维数组中? - How to return all array permutations iteratively into a two-dimensional array? 如何将列表中的元素添加到二维数组? - How to add elements from a list to a two-dimensional array? 如何在Java的二维字符串数组中查找唯一元素? - How to find unique elements in a two-dimensional array of strings in java? 如何访问二维数组上的所有插槽? - How to access all the slots on a two-dimensional array? 如何在另一个类的二维数组中写入只有一维数组的大小? - How to write in a two-dimensional array of another class the size of only one-dimensional array? 如何使二维数组的列总和行总和方法? - How to make sum of columns and sum of rows method for Two-dimensional array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM