长度不相等则补零,对应位置元素大的个数多的数组大
输入
2//数组长度
1 2
3
2 1 -1
输出
ture//数组1比数组2大的元素多
java在一个类中写方法比较两个int形数组大小
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
Tomshidi 2022-03-07 22:52关注public static void main(String[] args) { Scanner s = new Scanner(System.in); int countMax1 = 0; int countMax2 = 0; int arrSize1 = s.nextInt(); int[] arr1 = new int[arrSize1]; for (int i = 0; i < arrSize1; i++) { arr1[i] = s.nextInt(); } int arrSize2 = s.nextInt(); int[] arr2 = new int[arrSize2]; for (int i = 0; i < arrSize2; i++) { arr2[i] = s.nextInt(); } int loopSize = Math.max(arrSize1, arrSize2); for (int i = 0; i < loopSize; i++) { int arrItem1 = 0; int arrItem2 = 0; if (i < arrSize1) { arrItem1 = arr1[i]; } if (i < arrSize2) { arrItem2 = arr2[i]; } if (arrItem1 > arrItem2) { countMax1++; } if (arrItem2 > arrItem1) { countMax2++; } } System.out.println(countMax1 > countMax2); }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报无用 1