[英]An error saying "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1" shows up when I run my code
Here is my code:这是我的代码:
Scanner input= new Scanner (System.in);
int[][] anArray= new int[2][2];
int[][] anArray1= new int[2][2];
int[][] anArray2= new int[2][2];
int temp1=1, temp2=1;
for(int a=0; a<anArray.length; a++){
for(int b=0; b<anArray.length; b++){
System.out.println("Enter value in anArray index["+a+"]["+b+"]:");
anArray[a][b]= input.nextInt();
System.out.println("Enter value in anArray1 index["+a+"]["+b+"]:");
anArray[a][b]= input.nextInt();
}
for(int c=0; c<anArray2.length; c++){
for(int d=0; d<anArray2.length; d++){
anArray2[c][d]=anArray[c][d] + anArray1[temp1][temp2];
System.out.println("Total of "+ anArray[c][d]+ " and "+ anArray1[temp1][temp2]+ " is " + anArray2[c][d]);
temp2--;
}
temp1--;
temp2=1;
}
}
This is a laboratory from my class.这是我的 class 的实验室。 And once I simulated it, I don't quite get these parts of the code where it involved temp1 and temp2
一旦我模拟了它,我就不太明白涉及 temp1 和 temp2 的这些代码部分
int temp1=1, temp2=1;
anArray2[c][d]=anArray[c][d] + anArray1[temp1][temp2];
The line of error is here:错误行在这里:
anArray2[c][d]=anArray[c][d] + anArray1[temp1][temp2];
Please helpppp请帮助ppp
In this line:在这一行:
System.out.println("Enter value in anArray1 index["+a+"]["+b+"]:"); anArray[a][b]= input.nextInt();
You typed anArray[a][b] = input.nextInt();
你输入
anArray[a][b] = input.nextInt();
instead of anArray1[a][b] = input.nextInt();
而不是
anArray1[a][b] = input.nextInt();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.