在对象的 2D 数组中打印坐标标记.爪哇岛



>我正在尝试在 2D 数组上打印出坐标指示器...
我用另一个类来实例化数组上的对象。

我需要将坐标存储在两个(局部?)变量中,然后在打印的数组中显示这些坐标的位置。(已经用各种对象实例化了)

这是我到目前为止拥有的代码片段,但我无法让"C"打印在数组的正确位置。我厌倦的几个选项,要么根本不打印,要么打印左上角的"C"。

这是我厌倦的一个选项:此选项根本不打印"C"。

   private int cX=0;
   private int cY=0;
     //Randomly set coordinates on array.
       for(int i=0; i<array.length; i++){
            for(int j=0; j<array[i].length; j++){
                int x = randGen.nextInt(9);
                int y = randGen.nextInt(9);
                if (array [x][y].display()=='.'){
                    x=cX;
                    y=cY;
                }
             }
         }

    // print array
    private void displayArray()
    {
      for (int i = 0; i < array.length; i++){ 
        for (int j = 0; j < array[i].length; j++){
           if ((array [i][j].display()==cX)&&
                (array [i][j].display()==cY))
                System.out.print("C");
                System.out.print(board [i][j].display()+"t")   
         }
            System.out.println("n");
      }
    }
如果我

理解正确,您想将坐标保存到某种数据结构中,然后显示它们吗?

为此目的

创建一个类不是很合适吗? 例如一个名为 Coordinator 的类,用于保存 X 和 Y 值。

稍后使用X&Y值创建您创建的类的对象,并将其放入ArrayList中。

代码示例:

//Creating the a object holding 2 values
Coordinator cords = new Coordinator(randGen.nextInt(9), randGen.nextInt(9));
//Putting in the object to the data structure
List<Coordinator> list = new ArrayList<>();
list.add(cords);

说明和代码示例应该可以帮助您自己解决此问题。

此选项根本不打印"C"。

您当前的代码示例确实永远不会打印"C"。

for (int j = 0; j < array[i].length; j++){
if ((array [i][j].display()==cX)&&
       (array [i][j].display()==cY))

仅当 cX 等于 cY 并且两者都等于分配给数组的 ascii 代码值(对于 '." 这是值 46)时,此 if 条件才为真。但您可能只想找到 cX,cY 位置并打印一个"C"。您应该尝试使用当前的 i 和 j 值来组合您的位置(cX、cY)。

if ((i==cX)&&(j==cY))

打印左上角的"C"

您永远不会更改该位置的数组内容。您设置了 x 和 y,它们将被覆盖,以后无法访问

int x = randGen.nextInt(9);
int y = randGen.nextInt(9);
if (array [x][y].display()=='.'){
      x=cX;
      y=cY;
}

你想找到一个随机的位置让你的"C"出现,就像在棋盘游戏中一样吗?然后,您需要将找到的 x 和 y 坐标分配给 cX 和 cY。正确的值被分配给左侧变量。

cX = x;
cY = y;

我希望我理解正确,这很有帮助。

为了完整起见:我想我解决了。(好吧,无论如何它似乎都在工作)。感谢您的帮助!

  private int cX=0;
  private int cY=0;

  //Randomly set coordinates on the array. 
 for(int i=0; i<1; i++){
  int x = randGen.nextInt(9);
  int y = randGen.nextInt(9);
  if (board [x][y].display()=='.'){
   CX=x;
   CY=y;
  }
 }  

 //Print array
private void displayArray(){
 for (int i = 0; i < board.length; i++){ 
  for (int j = 0; j < board[i].length; j++){
   if ((i==CX)&&(j==CY))
    System.out.print("C"+"t");
   else
    System.out.print(board [i][j].display()+"t");
  }
 System.out.println("n");
 }
}
  private double[][] mLatLng = {
                {19.01062463, 73.01141475},
                {19.02369039, 73.00778391},};
    public void onMapReady(GoogleMap googleMap) { googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_icon);
    for (int i = 0; i < mLatLng.length; i++) {
    for (int j = 0; j < mLatLng[i].length; j++) {
    googleMap.addMarker(new MarkerOptions()
    .position(new LatLng(mLatLng[i][j], mLatLng[i][j]))
    .title("Location" + i).icon(bitmapDescriptorFromVector(context,R.drawable.ic_icon)));

最新更新