Java 我需要用x和y坐标在二维数组中找到值
这是我在代码中使用的网格,当我从扫描器中给出x和y坐标时,我需要找到坐标中的值Java 我需要用x和y坐标在二维数组中找到值,java,multidimensional-array,Java,Multidimensional Array,这是我在代码中使用的网格,当我从扫描器中给出x和y坐标时,我需要找到坐标中的值 int [][] grid = { {1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,1,1,1,0,1,0,0,0,0,0,1}, {1,0,1,0,0,0,1,0,1,1,1,0,1}, {1,0,0,0,1,1,1,0,0,0,0,0,1}, {1,0,1,0,1,1,1,0,1,0,1,0,1
int [][] grid = {
{1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,1,1,1,0,1,0,0,0,0,0,1},
{1,0,1,0,0,0,1,0,1,1,1,0,1},
{1,0,0,0,1,1,1,0,0,0,0,0,1},
{1,0,1,0,1,1,1,0,1,0,1,0,1},
{1,0,0,0,0,0,0,0,0,0,1,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1}
};
看起来很简单,或者您没有显示所有任务: 它将是: int targetValue=grid[x][y]您需要使用grid[y][x]
有时情况正好相反,但这取决于您。最简单的方法是通过:int userValue=grid[x][y] x值表示要查找的行,y值表示列 在使用scanner的情况下,代码应如下所示:
Scanner sc = new Scanner(System.in);
System.out.println("Enter x value: ");
int x = sc.nextInt();
System.out.println("Enter y value: ");
int y = sc.nextInt();
System.out.println(grid[x][y]);
那有什么问题?
Scanner sc = new Scanner(System.in);
System.out.println("Enter x value: ");
int x = sc.nextInt();
System.out.println("Enter y value: ");
int y = sc.nextInt();
System.out.println(grid[x][y]);