【发布时间】:2014-03-09 07:26:48
【问题描述】:
我定义了一个包含一些整数的二维数组。在我的程序中,用户输入一个数字以在二维数组中搜索它。找到数字后,我想打印数组中数字的位置。但在我的程序中,它无法打印 j 的位置。我该如何纠正?
public static void main(String[] args) {
int[][] arrayOfInt = {
{12, 14, 15},
{56, 36, 48},
{23, 78, 69,48}
};
Scanner input = new Scanner(System.in);
int search,i,j;
boolean check = false;
System.out.print("Enter your number: ");
search = input.nextInt();
search:
for (i=0; i<arrayOfInt.length; i++)
{
for(j=0; j<arrayOfInt[i].length; j++)
{
if(arrayOfInt[i][j] == search)
{
check = true;
break search;
}
}
}
if (check)
{
System.out.println("i = " + i + " and j = " + j);
}
else
{
System.out.println("There is not in the array!");
}
}
【问题讨论】:
-
为什么需要
search:? -
@MarounMaroun,一旦找到
j,它就是一个打破外循环的标签 -
@svz 是的,我知道,但这里是多余的。
-
@MarounMaroun 为什么它是多余的?