java - 数组兼容性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
** 帮帮我 **
解决方案
您的代码中有一些问题。
- 在您的 else 块中,您需要替换
(n1<1)
为(n2<1)
. - 替换
if(n1==n2)
为if(n1!=n2)
- 您不需要遍历所有元素。如果在任何时候你发现两个数组的索引元素都不满足条件,你应该打破循环。为此使用布尔标志。
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"); } } } }
推荐阅读
- android - kotlin中的lazy和lazyFast有什么区别?
- c# - 从 C# 中的列表中删除一个属性
- python - ValueError:输入数组应具有与目标数组相同数量的样本。在训练部分
- python - Python 通过串行到 Arduino,不能移动舵机
- reactjs - 我的表单不接受用户的输入值
- javascript - axios [Vue 警告] 中的 VueJS 错误:v-on 处理程序中的错误:“TypeError:this.axios.get(...).then(...).cache 不是函数”
- sql - PL/SQL 中参数化游标的意义何在?
- try-catch - 为什么 try catch 语句在 TwinCAT 4024.7 中不起作用
- reactjs - 如何仅为特定组件禁用浏览器后退按钮
- swift - 用户的 Swift 颜色选择器