Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
importjava.util.Scanner;publicclassmultiplication{publicstaticvoidmain(String[]args){...
import java.util.Scanner;
public class multiplication {
public static void main (String[] args){
Scanner input=new Scanner(System.in);
int a[][]=new int[2][3];
int b[][]={ {1,5,2,8},{5,9,10,-3},{2,7,-5,-18} };
int c[][]=new int[2][4];
int i,j,k;
System.out.println("Build Array a! ");
for( i=0;i<=a.length;i++){
for ( j=0;j<=a[i].length;j++){
a[i][j]=input.nextInt();
}
}//给a赋值
for( i=0; i<=c.length; i++ ){
for( j=0; j<=c[i].length; j++ ){
c[i][j]=0; }}//初始化数组C
for ( i=0;i<=a.length-1;i++){
for (k=0; k<=b[0].length-1; k++){
for (j=0;j<=b.length-1;j++){
c[i][k]=c[i][k]+a[i][j]*b[j][k];
}
}
}
System.out.println("*** Matrix A ***");
output(a);
System.out.println("*** Matrix B ***");
output(b);
System.out.println("*** Matrix C ***");
output(c);
}
public static void output(int array[][]){
//loop through array's row
for(int row=0; row<array.length; row++){
//loop through array's columns
for(int column=0; column<array[row].length; column++)
System.out.print(array[row][column]+" ");
System.out.println();
}
}
} 展开
public class multiplication {
public static void main (String[] args){
Scanner input=new Scanner(System.in);
int a[][]=new int[2][3];
int b[][]={ {1,5,2,8},{5,9,10,-3},{2,7,-5,-18} };
int c[][]=new int[2][4];
int i,j,k;
System.out.println("Build Array a! ");
for( i=0;i<=a.length;i++){
for ( j=0;j<=a[i].length;j++){
a[i][j]=input.nextInt();
}
}//给a赋值
for( i=0; i<=c.length; i++ ){
for( j=0; j<=c[i].length; j++ ){
c[i][j]=0; }}//初始化数组C
for ( i=0;i<=a.length-1;i++){
for (k=0; k<=b[0].length-1; k++){
for (j=0;j<=b.length-1;j++){
c[i][k]=c[i][k]+a[i][j]*b[j][k];
}
}
}
System.out.println("*** Matrix A ***");
output(a);
System.out.println("*** Matrix B ***");
output(b);
System.out.println("*** Matrix C ***");
output(c);
}
public static void output(int array[][]){
//loop through array's row
for(int row=0; row<array.length; row++){
//loop through array's columns
for(int column=0; column<array[row].length; column++)
System.out.print(array[row][column]+" ");
System.out.println();
}
}
} 展开
3个回答
展开全部
import java.util.Scanner;
public class multiplication {
public static void main (String[] args){
Scanner input=new Scanner(System.in);
int a[][]=new int[2][3];
int b[][]={ {1,5,2,8},{5,9,10,-3},{2,7,-5,-18} };
int c[][]=new int[2][4];
int i,j,k;
System.out.println("Build Array a! ");
for( i=0;i<=a.length-1;i++){
for ( j=0;j<=a[i].length-1;j++){
a[i][j]=input.nextInt();
}
}//给a赋值
for( i=0; i<=c.length-1; i++ ){
for( j=0; j<=c[i].length-1; j++ ){
c[i][j]=0; }}//初始化数组C
for ( i=0;i<=a.length-1;i++){
for (k=0; k<=b[0].length-1; k++){
for (j=0;j<=b.length-1;j++){
c[i][k]=c[i][k]+a[i][j]*b[j][k];
}
}
}
System.out.println("*** Matrix A ***");
output(a);
System.out.println("*** Matrix B ***");
output(b);
System.out.println("*** Matrix C ***");
output(c);
}
public static void output(int array[][]){
//loop through array's row
for(int row=0; row<array.length; row++){
//loop through array's columns
for(int column=0; column<array[row].length; column++)
System.out.print(array[row][column]+" ");
System.out.println();
}
}
}
以上是修改后的代码.错误在于给a还有c赋值的时候for循环条件写错了,导致数组越界了.
public class multiplication {
public static void main (String[] args){
Scanner input=new Scanner(System.in);
int a[][]=new int[2][3];
int b[][]={ {1,5,2,8},{5,9,10,-3},{2,7,-5,-18} };
int c[][]=new int[2][4];
int i,j,k;
System.out.println("Build Array a! ");
for( i=0;i<=a.length-1;i++){
for ( j=0;j<=a[i].length-1;j++){
a[i][j]=input.nextInt();
}
}//给a赋值
for( i=0; i<=c.length-1; i++ ){
for( j=0; j<=c[i].length-1; j++ ){
c[i][j]=0; }}//初始化数组C
for ( i=0;i<=a.length-1;i++){
for (k=0; k<=b[0].length-1; k++){
for (j=0;j<=b.length-1;j++){
c[i][k]=c[i][k]+a[i][j]*b[j][k];
}
}
}
System.out.println("*** Matrix A ***");
output(a);
System.out.println("*** Matrix B ***");
output(b);
System.out.println("*** Matrix C ***");
output(c);
}
public static void output(int array[][]){
//loop through array's row
for(int row=0; row<array.length; row++){
//loop through array's columns
for(int column=0; column<array[row].length; column++)
System.out.print(array[row][column]+" ");
System.out.println();
}
}
}
以上是修改后的代码.错误在于给a还有c赋值的时候for循环条件写错了,导致数组越界了.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
数组下标越界,
应该是,你的数组里只有7个元素,也就是说a[0-6],可是你循环的时候,出现了a[7]的情况。
你再检查一下吧。
应该是,你的数组里只有7个元素,也就是说a[0-6],可是你循环的时候,出现了a[7]的情况。
你再检查一下吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询