Tic tac toe array问题 [英] Tic tac toe array issue

查看:76
本文介绍了Tic tac toe array问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对一个tic tac toe board的阵列有点麻烦。我是否能够使用我所拥有的数组代码并使用所需的变量进行更改,或者是否需要创建一个全新的数组。



我尝试过:



I am having a little trouble with an array for a tic tac toe board. Would I be able to use the array code that I have and alter it with what variables are needed or does a whole new array need to be made.

What I have tried:

public int[][] grid(int rSize, int cSize)
    {
        
        array = new int[rSize][cSize];
        for(row = 0; row < array.length; row++)
        {
            for(col = 0; col < array[0].length; col++)
            {
                array[row][col] = 0;
            }
        }
        return array;
    }

推荐答案

也许你可以尝试在循环中使用数组的维度,如果这是问题?

Maybe you could try to use the dimensions of the array inthe loop, if that is the problem?
public int[][] grid(int rSize, int cSize)
{
   array = new int[rSize][cSize];
   for(row = 0; row < rSize; row++)
   {
      for(col = 0; col < cSize; col++)
      {
         array[row][col] = 0;
      }
   }
   return array;
}



但是,因为整数数组无论如何都会用零值初始化,并且因为该方法只是创建并返回数组,所以它会更简单并且更快地摆脱该方法。



假设您正在使用这种方法:


But, since an integer array will be initialized with zero values anyway, and since the method is only creating and returning the array, it would be simpler and quicker to get rid of the method.

Assuming you are using the method this way:

int[][] myArray = grid(3, 3);



你可以摆脱这个方法并简单地写一下


you could get rid of the method and simply write

int[][] myArray = new int[3][3];



技术上有如果您希望它分配特定的初始值,那么这只会对该方法感兴趣;这不是这里的情况。



希望这有帮助。


Technically there would only be an interest for the method if you would want it to assign specific initial values; which is not the case here.

Hope this helps.


这篇关于Tic tac toe array问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆