来自用户输入的两个数组并获得两个数组的公共值的输出
two arrays from user input and get an output of the common values of the two arrays
我还是 java 的新手,我一直在尝试编写代码,采用两个不同的公共值数组并输出两个数组的公共值,但我不断收到以下错误消息:
Exception in thread "main" Your common values are: 0 Your common
values are: 0 Your common values are: 0 Your common values are: 0
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for
length 5 at HomworkTestingQ.main(HomworkTestingQ.java:18)
Scanner sc = new Scanner(System.in);{
int n = 5;
int m = 5;
int[] array1 = new int[m];
int[] array2 = new int[n];
System.out.println("Enter the first array: ");
n=sc.nextInt();
System.out.println("Enter the second array");
m=sc.nextInt();
for(int i = 0; i < array1.length; i++) {
for(int j = 0; i < array2.length; j++) {
if(array1[i] == array2[j]) {
System.out.println("Your common values are: " + array1[i+j] );
}
}
}
}
}
}
我认为问题在于您要在此处添加数组迭代器:
array1[i+j]
i+j相加超过了array1的长度
另外,您的数组没有按照我认为的那样填充,基于:
System.out.println("Enter the first array: ");
n=sc.nextInt();
System.out.println("Enter the second array");
m=sc.nextInt();
我只是猜测也许您接下来还有更多工作要做。
你不需要把指数加起来..
由于 array1[i] 等于 array2[j],打印其中任何一个:
for(int i=0;i<array1.length;i++){
for(int j=i+1;j<array2.length;j++){
if(array1[i]==array2[j]) int commonValue = array1[i];
return commonValue; // or print it
}
}
第一个问题
当你扫描 m
和 n
的值时,数组的大小不会改变,因为 java 是按值传递的,数组的大小是值,而不是变量。
所以你应该做这样的事情-
int m = scanner.nextInt();
int[] arr = new int[m];
第二个问题
System.out.println("Your common values are: " + array1[i+j] );
这会越界,也许你应该这样做-
System.out.println("Your common values are: " + array1[i] );
我修改你的代码:
Scanner sc = new Scanner(System.in);
int n = 5;
int m = 5;
int[] array1 = new int[m];
int[] array2 = new int[n];
System.out.println("Enter the first array: ");
for (int i = 0; i < n; i++) {
array1[i] = sc.nextInt();
}
System.out.println("Enter the second array");
for (int i = 0; i < n; i++) {
array2[i] = sc.nextInt();
}
for (int item : array1) {
for (int value : array2) {
if (item == value) {
System.out.println("Your common values are: " + item);
}
}
}
我还是 java 的新手,我一直在尝试编写代码,采用两个不同的公共值数组并输出两个数组的公共值,但我不断收到以下错误消息:
Exception in thread "main" Your common values are: 0 Your common values are: 0 Your common values are: 0 Your common values are: 0 java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 at HomworkTestingQ.main(HomworkTestingQ.java:18)
Scanner sc = new Scanner(System.in);{
int n = 5;
int m = 5;
int[] array1 = new int[m];
int[] array2 = new int[n];
System.out.println("Enter the first array: ");
n=sc.nextInt();
System.out.println("Enter the second array");
m=sc.nextInt();
for(int i = 0; i < array1.length; i++) {
for(int j = 0; i < array2.length; j++) {
if(array1[i] == array2[j]) {
System.out.println("Your common values are: " + array1[i+j] );
}
}
}
}
}
}
我认为问题在于您要在此处添加数组迭代器:
array1[i+j]
i+j相加超过了array1的长度
另外,您的数组没有按照我认为的那样填充,基于:
System.out.println("Enter the first array: ");
n=sc.nextInt();
System.out.println("Enter the second array");
m=sc.nextInt();
我只是猜测也许您接下来还有更多工作要做。
你不需要把指数加起来.. 由于 array1[i] 等于 array2[j],打印其中任何一个:
for(int i=0;i<array1.length;i++){
for(int j=i+1;j<array2.length;j++){
if(array1[i]==array2[j]) int commonValue = array1[i];
return commonValue; // or print it
}
}
第一个问题
当你扫描 m
和 n
的值时,数组的大小不会改变,因为 java 是按值传递的,数组的大小是值,而不是变量。
所以你应该做这样的事情-
int m = scanner.nextInt();
int[] arr = new int[m];
第二个问题
System.out.println("Your common values are: " + array1[i+j] );
这会越界,也许你应该这样做-
System.out.println("Your common values are: " + array1[i] );
我修改你的代码:
Scanner sc = new Scanner(System.in);
int n = 5;
int m = 5;
int[] array1 = new int[m];
int[] array2 = new int[n];
System.out.println("Enter the first array: ");
for (int i = 0; i < n; i++) {
array1[i] = sc.nextInt();
}
System.out.println("Enter the second array");
for (int i = 0; i < n; i++) {
array2[i] = sc.nextInt();
}
for (int item : array1) {
for (int value : array2) {
if (item == value) {
System.out.println("Your common values are: " + item);
}
}
}