首页 > 解决方案 > 数组兼容性java

问题描述

这些是我的指示:

如果两个数组具有相同的大小并且如果第一个数组中的第 i 个元素对于所有元素都大于或等于第二个数组中的第 i 个元素,则称这两个数组是兼容的。如果数组大小为零或更小,则显示消息“无效数组大小”。编写一个 Java 程序来判断 2 个数组是否兼容。如果阵列兼容,则将消息显示为“阵列兼容”,否则将消息显示为“阵列不兼容”。

我的解决方案如下:

class CompaibleArrays {
    public static void main(String []args){
        int n1=0;
        int n2=0;
        int flag=0;
        int []a1 = new int[20];
        int []a2 = new int[20];
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the size for First array:");
        n1 = sc.nextInt();
        if(n1<1){
            System.out.println("Invalid array size");
        }
        else{
            System.out.println("Enter the elements for First array:");
            for(int i=0 ; i<n1 ; i++){
                a1[i] = sc.nextInt();
            }

            System.out.println("Enter the size for Second array:");
            n2 = sc.nextInt();
            if(n1<1){
                System.out.println("Invalid array size");
            }
            else{
                System.out.println("Enter the elements for Second array:");
                for(int j=0 ; j<n2 ; j++){
                    a2[j] = sc.nextInt();
                }
            }
        }
        if(n1==n2){
            System.out.println("Arrays are Not Compatible");
        }
        else{
            for(int x=0 ; x<n1 ; x++){
                if(a1[x]>=a2[x]){
                    flag++;
                }
            }
            if(flag==n1){
                System.out.println("Arrays are Compatible");
            }
            else{
                System.out.println("Arrays are Not Compatible");
            }
        }

    }
} 

样本输入 1:

 Enter the size for First array:
 5
 Enter the elements for First array:
 5
 14
 17
 19
 15
 Enter the size for Second array:
 5
 Enter the elements for Second array:
 2
 5
 9
 15
 7
 Sample Output 1:
 Arrays are Compatible

样本输入 2:

 Enter the size for First array:
 3
 Enter the elements for First array:
 1
 4
 7
 Enter the size for Second array:
 5
 Enter the elements for Second array:
 2
 5
 9
 5
 7
 Sample Output 2:
 Arrays are Not Compatible

样本输入 3:

 Enter the size for First array:
 -2
 Sample Output 3:
 Invalid array size

** 帮帮我 **

标签: javaarrays

解决方案


您的代码中有一些问题。

  1. 在您的 else 块中,您需要替换(n1<1)(n2<1).
  2. 替换if(n1==n2)if(n1!=n2)
  3. 您不需要遍历所有元素。如果在任何时候你发现两个数组的索引元素都不满足条件,你应该打破循环。为此使用布尔标志。
public class CompaibleArrays {
    public static void main(String []args){
        int n1=0;
        int n2=0;
        boolean flag=true;
        int []a1 = new int[20];
        int []a2 = new int[20];
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the size for First array:");
        n1 = sc.nextInt();
        if(n1<1){
            System.out.println("Invalid array size");
        }
        else{
            System.out.println("Enter the elements for First array:");
            for(int i=0 ; i<n1 ; i++){
                a1[i] = sc.nextInt();
            }

            System.out.println("Enter the size for Second array:");
            n2 = sc.nextInt();
            if(n2<1){
                System.out.println("Invalid array size");
            }
            else{
                System.out.println("Enter the elements for Second array:");
                for(int j=0 ; j<n2 ; j++){
                    a2[j] = sc.nextInt();
                }
            }
        }
        if(n1!=n2){
            System.out.println("Arrays are Not Compatible");
        }
        else{
            for(int x=0 ; x<n1 ; x++){
                if(a1[x]<a2[x]){
                    flag = false;
                    break;
                }
            }
            if(flag==true){
                System.out.println("Arrays are Compatible");
            }
            else{
                System.out.println("Arrays are Not Compatible");
            }
        }

    }
}

推荐阅读