简体   繁体   中英

How to pass variables with 2 2D Arrays in Java?

I am having issues passing arrays to my methods it is getting an error of "cannot find symbol"but I am confused on how these 2D arrays should be passed in the method "how many" This program is supposed to prompt for 18 ints and placed into 2 2D arrays then return of they are equal or not. Am I supposed to place the array names in boolean equalOrNot = howmany(FirstArray, SecondArray); or what?

import java.util.Scanner;
public class n01092281
{
    public static void main (String[] args) 
   {
      Scanner input = new Scanner (System.in);

      int FirstArray [][] = new int[3][3];
      int SecondArray [][] = new int[3][3];

      System.out.print("Enter List1 and List2 (18 numbers): ");
      for (int row = 0; row < FirstArray.length; row++)
      {
          for(int column = 0; column < FirstArray[row].length; column++)
          {
              FirstArray[row][column] = input.nextInt();
          }
      }
      for (int row = 0; row < SecondArray.length; row++)
      {
          for(int column = 0; column < SecondArray[row].length; column++)
          {
              SecondArray[row][column] = input.nextInt();
          }
      }        
      boolean equalOrNot = howmany(FirstArray, SecondArray);   
         if (equalOrNot)
        {
            System.out.println("Two Arrays Are Equal");
        }
        else
        {
            System.out.println("Two Arrays Are Not equal");
        }  



   }   
   public class strict 
   { 
      public boolean howmany(int[][] FirstArray, int[][] SecondArray)
      {
         boolean equalOrNot = true;

        if(FirstArray.length == SecondArray.length)
        {
            for (int i = 0; i < FirstArray.length; i++)
            {
                if(FirstArray[i] != SecondArray[i])
                {
                    equalOrNot = false;
                }
            }
        }
        else
        {
            equalOrNot = false;
        }




      }   
     }

  }

Few points that could help over the shared code :

  1. Please follow better-naming conventions.

  2. public boolean howmany does not have a return statement to actually return a boolean value, possibly add at the end of the method :

     return equalOrNot; 
  3. The comparison should also be over the number of columns of the array and the condition should become somewhat like :

     if(FirstArray[i][j] != SecondArray[i][j]) 
  4. You cannot call howmany method currently from a static context even considering Strict to be an inner class(with those inappropriately matched braces).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM