如何在输入的二维数组java中找到奇数的数量和所有奇数的总和?

How to find the amount of odd numbers and thee sum of all odd numbers in a inputted 2D array java?

我正在尝试查找用户在 java 中输入的二维数组中奇数的总数。我还试图找到所有这些奇数的总和。我已经想出如何在严格的方形二维数组(即 2x2、3x3、4x4 等)中找到奇数的数量,但是当我输入一个数组大小时,例如 2x3 或 5x6,输出不正确。我做错了什么?

import java.util.Scanner;
public class SumOfOdd
{
    public static void main (String []args)
    {
        Scanner input =  new Scanner (System.in);
        System.out.println("No. of rows");
        int rows = input.nextInt();
        System.out.println("No. of columns");
        int cols = input.nextInt();

        int [][] array1 = new int[rows][cols];
        System.out.println("Input array elements");
        for(int row = 0; row < rows; row++)
        {
            for(int col = 0; col < cols; col++)
            {
                array1[row][col] = input.nextInt();
            }
        }
        int count = 0;
        for(int i = 0; i < array1.length; i++)
        {
            for(int j = 0; j < array1.length; j++)
            {
                if(array1[i][j] % 2 == 1)
                {
                    count++;
                }
            }
        }
        System.out.println("Odd number count = " + count);
    }
}

替换

for(int j = 0; j < array1.length; j++)

for(int j = 0; j < array1[i].length; j++)

替换最后的嵌套for循环

for(int i = 0; i < array1.length; i++) { for(int j = 0; j < array1.length; j++){

for(int i=0;i<rows;i++) { for(int j=0;j<cols;j++) {