从阵列计数和打印

时间:2015-03-30 23:08:59

标签: java arrays for-loop

我有一项任务是创建一个tic tac toe board并使用Random填充X,O或空格。填充数组后,我必须使用一种方法来计算方法的每一行中的每个X或O,例如。第1行有2个X和1个O.我已经填充了阵列,但我不知道如何使用for循环来计算X或O的数量。数组的每一行。在我做行之后,我还必须做列和对角线,所以现在忽略这些方法,谢谢

public class Arrayexample {

    public static void main(String[] args) 
    {
        int size;
        char array[][]; 
        Random randy = new Random();
        for (size = 4; size <= 4; size++)
        {
            //create array
            array = fillArray(size, randy);
            printArray(array);
            doRows(array);
            doColumns(array);
            doDiagonals(array);
            //print array
            System.out.println("\n\t\tsize " + size); 
        }
    }

    static char [][] fillArray (int size, Random randy)
    {
        int row,col,N;
        char array[][];
        // create array
        array = new char[size][size];
        //code inside loops to choose X, O or space with weighting
        for(row=0;row<array.length;row++)
        {
            for (col=0;col<array.length;col++)
            {
                N = randy.nextInt(5);
                array[row][col] =
                N == 0 || N == 1 ? 'X' : N == 2 || N == 3 ? 'O' : ' ';
            }
        }
        return array;
    }    

    static void printArray (char a [][])
    { 
        int row, col;        
        for(row=0; row < a.length; row++) {
            for(col=0; col < a[0].length; col++) {
                System.out.print(a[row][col] +" " );
            }
            System.out.println();
        }

    }
    static void doRows (char a [][])
    {
        int [][] counts; 
        counts = new int [2][2]; 
        getRows(a,counts); 
        printRows(a,counts); 
    }    
    static void getRows (char a [][], int counts [][])
    {
        int row,countsRow; 
        for(row=0; row < a.length; row++)
        {
            for (countsRow =0; countsRow<a.length; countsRow++)
            {

            }    
            System.out.println("row " + (row + 1)); 
        }
    }
    static void printRows (char a [][], int counts [][])
    {

    }

    static void doColumns (char a [][])
    {
        int counts [][]; 
        counts = new int [2][2];
        getColumns(a,counts); 
        printColumns(a,counts); 
    }

    static void getColumns (char a [][], int counts [][])
    {
        int col, countsCol; 
        for (countsCol = 0; countsCol<a.length; countsCol ++ )
        {

        }
    }

    static void printColumns (char a [][], int counts [][])
    {

    }

    static void doDiagonals(char a [][])
    {
        int counts [][];    
        counts = new int [2][2];
        getDiagonals(a,counts); 
        printDiagonals(a,counts); 

    }

    static void getDiagonals(char a [][], int counts [][])
    {

    }

    static void printDiagonals (char a [][], int counts [][])
    {

    }
}

1 个答案:

答案 0 :(得分:0)

尝试使用行并适应其他行:

for(int i = 0; i < size; i++){
    int countX = 0;
    int countO = 0;
    for(int j = 0; j < size; j++){
           if(array[i][j] == 'X')
            countX++;
        else
            countO++;
    }
    System.out.println("row: " + i + " " + countX + "X " + countO + "O");
}