Basically, 开发者_开发技巧user have to specify how the many row and column they want [n*n] and after that user will input value and it will store in multidimension array.I run the code but getting error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Total.main(Total.java:30)
Code:
import java.util.*;
public class Total
{
public static void main(String args[])
{
int n,value;
Scanner input = new Scanner(System.in);
System.out.println("Enter integer for row and column:");
n = input.nextInt();
int arrX[][] = new int [n][n];
for(int i=0; i < n; i++ )
{
for(int j=0; j < n; i++)
{
System.out.printf("Enter integer [%d],[%d]:",i,j);
value = input.nextInt();
arrX[i][j] = value;
}
}
}
}
In the line:
for(int j=0; j < n; i++)
you increase i
where it should be j
like this:
for(int j=0; j < n; j++)
change this line:
for(int j=0; j < n; **j**++)
it's a typo perhaps
You are incrementing i
in both loops.
import java.util.*;
public class Total
{
public static void main(String args[])
{
int n,value;
Scanner input = new Scanner(System.in);
System.out.println("Enter integer for row and column:");
n = input.nextInt();
int arrX[][] = new int [n][n];
for(int i=0; i < n; i++ )
{
for(int j=0; j < n; j++)
{
System.out.printf("Enter integer [%d],[%d]:",i,j);
value = input.nextInt();
arrX[i][j] = value;
}
}
}
}
In your second loop you do i++
instead of j++
The problem is you increment the wrong variable in the second for:
for(int j=0; j < n; i++)
Instead of i you must type j
Works fine if you change this.
Cheers
精彩评论