最大和最小程序的数组索引越界异常
Array index out of bound exception for Maximum and minimum program
我正在编写一个程序来接受二维数组中的数字并找到最大和最小的数字。
但是当我输入 mu 输入时,它在第二个 if 语句中显示错误:
"Array index out of bound exception"
import java.util.Scanner;
public class DDA_MaxMin
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int ar[][] = new int[4][4];
int a,b,c=0,d=0;
for(a=0;a<4;a++)
{
for(b=0;b<4;b++)
{
System.out.println("Enter the numbers in the matrix "+a+" "+b);
ar[a][b]=in.nextInt();
}
}
c=ar[0][0];
d=ar[0][0];
for(a=0;a<4;a++)
{
for(b=0;b<4;b++)
if(c>ar[a][b])
c=ar[a][b];
if(d<ar[a][b])
d=ar[a][b];
}
System.out.println("The greatest number is "+d);
System.out.println("The smallest number is "+c);
}
}
没有{
的for循环只对下一行或下一条语句有效
for(b = 0; b < 4; b++)
if(c>ar[a][b])
c=ar[a][b]
此后 b
值为 4
。
之后的 if 语句超出了 for 循环,因此出现了越界异常。
用大括号括起来。
for(a=0;a<4;a++)
{
for(b=0;b<4;b++){
if(c>ar[a][b])
c=ar[a][b];
if(d<ar[a][b])
d=ar[a][b];
}
}
嘿,你错过了第二个大括号,因为正确的代码是
问题出在处理变量 b
的第二个 for
循环中。它缺少牙套。将其更改为:
for(a=0;a<4;a++) {
for(b=0;b<4;b++) {
if(c>ar[a][b])
c=ar[a][b];
if(d<ar[a][b])
d=ar[a][b];
}
}
`
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int ar[][] = new int[4][4];
int a,b,c=0,d=0;
for(a=0;a<4;a++)
{
for(b=0;b<4;b++)
{
System.out.println("Enter the numbers in the matrix "+a+" "+b);
ar[a][b]=in.nextInt();
}
}
c=ar[0][0];
d=ar[0][0];
for(a=0;a<4;a++)
{
for(b=0;b<4;b++)
{
if(c>ar[a][b])
c=ar[a][b];
if(d<ar[a][b])
d=ar[a][b];
}
}
System.out.println("The greatest number is "+d);
System.out.println("The smallest number is "+c);
}`
您在尝试查找最大和最小整数的 for 循环中缺少括号。
我正在编写一个程序来接受二维数组中的数字并找到最大和最小的数字。 但是当我输入 mu 输入时,它在第二个 if 语句中显示错误:
"Array index out of bound exception"
import java.util.Scanner;
public class DDA_MaxMin
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int ar[][] = new int[4][4];
int a,b,c=0,d=0;
for(a=0;a<4;a++)
{
for(b=0;b<4;b++)
{
System.out.println("Enter the numbers in the matrix "+a+" "+b);
ar[a][b]=in.nextInt();
}
}
c=ar[0][0];
d=ar[0][0];
for(a=0;a<4;a++)
{
for(b=0;b<4;b++)
if(c>ar[a][b])
c=ar[a][b];
if(d<ar[a][b])
d=ar[a][b];
}
System.out.println("The greatest number is "+d);
System.out.println("The smallest number is "+c);
}
}
没有{
的for循环只对下一行或下一条语句有效
for(b = 0; b < 4; b++)
if(c>ar[a][b])
c=ar[a][b]
此后 b
值为 4
。
之后的 if 语句超出了 for 循环,因此出现了越界异常。
用大括号括起来。
for(a=0;a<4;a++)
{
for(b=0;b<4;b++){
if(c>ar[a][b])
c=ar[a][b];
if(d<ar[a][b])
d=ar[a][b];
}
}
嘿,你错过了第二个大括号,因为正确的代码是
问题出在处理变量 b
的第二个 for
循环中。它缺少牙套。将其更改为:
for(a=0;a<4;a++) {
for(b=0;b<4;b++) {
if(c>ar[a][b])
c=ar[a][b];
if(d<ar[a][b])
d=ar[a][b];
}
}
`
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int ar[][] = new int[4][4];
int a,b,c=0,d=0;
for(a=0;a<4;a++)
{
for(b=0;b<4;b++)
{
System.out.println("Enter the numbers in the matrix "+a+" "+b);
ar[a][b]=in.nextInt();
}
}
c=ar[0][0];
d=ar[0][0];
for(a=0;a<4;a++)
{
for(b=0;b<4;b++)
{
if(c>ar[a][b])
c=ar[a][b];
if(d<ar[a][b])
d=ar[a][b];
}
}
System.out.println("The greatest number is "+d);
System.out.println("The smallest number is "+c);
}`
您在尝试查找最大和最小整数的 for 循环中缺少括号。