为多个数组中的方法嵌套for循环,java

时间:2016-09-23 18:47:45

标签: java arrays

我的代码在运行时如何返回false?

//Main
public class blah {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Strict run = new Strict();

        //Input
        int[][] m1 = new int[3][3];
        int[][] m2 = new int[3][3];

        System.out.print("Enter 9 numbers for List 1 / Enter 9 numbers for List 2\n");

        for (int arr1 = 0; arr1 < m1.length; arr1++) {
            for (int arr2 = 0; arr2 < m1[arr1].length; arr2++) {
                m1[arr1][arr2] = input.nextInt();
            }
        }

        for (int arr1 = 0; arr1 < m2.length; arr1++) {
            for (int arr2 = 0; arr2 < m2[arr1].length; arr2++) {
                m1[arr1][arr2] = input.nextInt();
            }
        }

        //The arrays are strictly equal
        System.out.print("");
        System.out.print("The arrays are strictly equal: " + run.equals(m1, m2));
    }
}

class Strict {
    //1.) The arrays are strictly equal
    public static boolean equals(int[][] m1, int[][] m2) {
        boolean isitEqual = false;
        for (int i = 0; i < m1.length; i++) {
            for (int j = 0; j < m1.length; j++) {

                if (m1[i][j] != m2[i][j]) {
                    return false;
                }
            }
        }
        return isitEqual;
    }
}

我的问题是否有问题?或者它是嵌套的for循环? 结果如下:

  

为列表1输入9个数字/为列表2输入9个数字   1 2 3 4 5 6 7 8 9
  1 2 3 4 5 6 7 8 9

     

数组严格相等:false

*********************更新:代码已更正********************** *****************

//Library Imports
import java.util.Scanner;
import java.util.Arrays;


//Main
public class blah
{
   public static void main (String[] args)
   {
   Scanner input = new Scanner(System.in);
   Strict run = new Strict();


   //Input
   int [][] m1 = new int[3][3];
   int [][] m2 = new int[3][3];

   System.out.print("Enter 9 numbers for List 1 / Enter 9 numbers for List 2\n");

   for (int arr1 = 0; arr1 < m1.length; arr1++)
      {
         for (int arr2 = 0; arr2 < m1[arr1].length; arr2++)
         {
            m1[arr1][arr2] = input.nextInt();
         }
      }

  for (int arr1 = 0; arr1< m2.length; arr1++)
      {
         for (int arr2 = 0; arr2<m2[arr1].length; arr2++)
         {
            m2[arr1][arr2] = input.nextInt();
         }
      }

   //The arrays are strictly equal
   System.out.print("");
   System.out.print("The arrays are strictly equal: " + run.equals(m1,m2));
    }
}
class Strict
{
//1.) The arrays are strictly equal
   public static boolean equals(int [][]m1, int [][]m2)
   {
        boolean isitEqual = true;
        for (int i = 0; i < m1.length; i++)
        {
            for (int j = 0; j < m1.length; j++)
            {

                if (m1[i][j] != m2[i][j])
                {
                  return false;
                }

            }
        }
    return isitEqual;

   }
}

2 个答案:

答案 0 :(得分:0)

您最初将isitEqual设置为false,并且在equals()方法中的任何位置都不会将其更改为true。

您可以替换

return isitEqual;

return true; 

然后删除

boolean isitEqual = false;

所有在一起或者您可以在初始化时将其设置为true:

boolean isitEqual = true;

答案 1 :(得分:0)

你有一个if语句检查if (m1[i][j] != m2[i][j])并返回false,你可以添加else { return true; }或只是用boolean isitEqual = false <只更改你的默认boolean isitEqual = true < / p>