Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 打印二维数组中值的位置_Java_Arrays_Search - Fatal编程技术网

Java 打印二维数组中值的位置

Java 打印二维数组中值的位置,java,arrays,search,Java,Arrays,Search,我定义了一个包含一些整数的二维数组。在我的程序中,用户输入一个数字以在二维数组中搜索它。找到号码后,我想打印号码在数组中的位置。但在我的程序中,它无法打印j的位置。我怎样才能纠正它 public static void main(String[] args) { int[][] arrayOfInt = { {12, 14, 15}, {56, 36, 48}, {23, 78, 69,48} }; Scanner input = ne

我定义了一个包含一些整数的二维数组。在我的程序中,用户输入一个数字以在二维数组中搜索它。找到号码后,我想打印号码在数组中的位置。但在我的程序中,它无法打印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!");
   }
}
publicstaticvoidmain(字符串[]args){
int[][]数组查找={
{12, 14, 15},
{56, 36, 48},
{23, 78, 69,48}
};
扫描仪输入=新扫描仪(System.in);
int搜索,i,j;
布尔检查=假;
System.out.print(“输入您的号码:”);
search=input.nextInt();
搜索:

for(i=0;i编译器抱怨
j
没有初始化,因为只有在执行外部for循环的内容时才会给它赋值

您可以通过将
j
初始化为任意值来消除此错误,如下所示:

int search, i, j = -1;

你的程序看起来不错,应该没有任何问题

唯一的问题是,为了打印数组的实际索引,您需要打印i+1和j+1值。此外,您还需要在开始时初始化j

int search,i,j = 0;

if (check)
{
     System.out.println("i = " + (i+1) + " and j = " + (j+1));
}

为什么需要
搜索:
?@marounnaroun,它是一个标签,一旦
j
被激活,就可以打破外部循环found@svz是的,我知道,但这里是多余的。@marounnaroun为什么是多余的?