第八章第二十八题(严格相等的数组)(Strictly equal array)

#第八章第二十八题(严格相等的数组)(Strictly equal array)

  • 8.28(严格相等的数组)如果两个二维数组m1和m2相应的元素相等的话,则认为它们是严格相同的。编写一个方法,如果m1和m2是严格相同的话,返回true。使用下面的方法头:
    public static boolean equals(int[][] m1,int[][] m2)
    编写一个测试程序,提示用户输入了两个3x3的整数数组。显示两个矩阵是否是严格相同的。下面是一个运行示例:
    Enter list1:51 22 25 6 1 4 24 54 6
    Enter list2:51 22 25 6 1 4 24 54 6
    The two arrays are strictly identical
    Enter list1:51 22 25 6 1 4 24 54 6
    Enter list2:51 25 22 6 1 4 24 54 6
    The two arrays are not strictly identical
    8.28(Strictly equal array)If the corresponding elements of two two-dimensional arrays M1 and M2 are equal, they are considered to be strictly the same. Write a method that returns true if M1 and M2 are exactly the same. Use the following method header:
    public static boolean equals(int[][] m1,int[][] m2)
    Write a test program to prompt the user to input two 3x3 integer arrays. Shows whether the two matrices are exactly the same. Here is a running example:
    Enter list1:51 22 25 6 1 4 24 54 6
    Enter list2:51 22 25 6 1 4 24 54 6
    The two arrays are strictly identical
    Enter list1:51 22 25 6 1 4 24 54 6
    Enter list2:51 25 22 6 1 4 24 54 6
    The two arrays are not strictly identical

  • 参考代码:

    package chapter08;
    
    import java.util.Scanner;
    
    public class Code_28 {
        public static void main(String args[]){
            int[][] num1 = new int[3][3];
            int[][] num2 = new int[3][3];
            Scanner cin = new Scanner(System.in);
            System.out.print("Enter list1:");
            for(int i = 0; i < 3; ++i)
                for(int j = 0; j < 3; ++j){
                    num1[i][j] = cin.nextInt();
                }
    
            System.out.print("Enter list2:");
            for(int i = 0; i < 3; ++i)
                for(int j = 0; j < 3; ++j){
                    num2[i][j] = cin.nextInt();
                }
            if (equals(num1,num2))
                System.out.println("The two arrays are strictly identical");
            else
                System.out.println("The two arrays are not strictly identical");
        }
        public static boolean equals(int[][] m1,int[][] m2){
            boolean flag = true;
            for(int i = 0; i < 3; ++i){
                for(int j = 0; j < 3; ++j){
                    if(m1[i][j] != m2[i][j]){
                        flag = false;
                        break;
                    }
                }
            }
            return flag;
        }
    }
    
    
  • 结果显示:

    Enter list1:51 22 25 6 1 4 24 54 6
    Enter list2:51 25 22 6 1 4 24 54 6
    The two arrays are not strictly identical
    
    Process finished with exit code 0
    
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值