计数和从阵列打印 [英] counting and printing from an array

查看:109
本文介绍了计数和从阵列打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分配创建一个井字板,并与X,O或使用随机空间填充板。数组被填满后我必须用一种方法来计算每个X或澳方法,EX的各项一行。第1行有2个X和1 O.我已经填补了数组,但我不知道如何使用一个for循环来统计数组中的每一行X的或O公司的数量。我这样做行以后我也要做列和对角线所以忽略这些方法从目前来看,由于

 公共类Arrayexample {    公共静态无效的主要(字串[] args)
    {
        INT大小;
        字符数组[] [];
        随机兰迪=新的随机();
        对于(大小= 4;大小< = 4;大小++)
        {
            //创建数组
            阵列= fillArray(大小,兰迪);
            printArray(数组);
            doRows(数组);
            doColumns(数组);
            doDiagonals(数组);
            //打印阵列
            的System.out.println(\\ n \\ t \\ TSIZE+大小);
        }
    }    静态的char [] [] fillArray(INT大小,随机兰迪)
    {
        INT行,列,N;
        字符数组[] [];
        //创建数组
        阵列=新的char [大小] [SIZE]
        // code内循环,以选择X,O或与加权空间
        为(行= 0;&行LT; array.length;排++)
        {
            对于(COL = 0;&山坳下,array.length;西++)
            {
                N = randy.nextInt(5);
                数组[行] [山口] =
                ñ== 0 || ñ== 1? 'X':N = = || 2 ñ== 3? 'O':'';
            }
        }
        返回数组;
    }    静态无效printArray(一个烧焦[] [])
    {
        INT行,列;
        为(行= 0;&行下,则为a.length;排++){
            为(COL = 0; COL&下;一个[0]。长度;山口++){
                System.out.print(A [行] [COL] +);
            }
            的System.out.println();
        }    }
    静态无效doRows(一个烧焦[] [])
    {
        INT [] []计数;
        计数=新INT [2] [2];
        GetRows的(一,计数);
        printRows(一,计数);
    }
    静态无效GetRows的(一个烧焦[] [],诠释计数[] [])
    {
        INT行,countsRow;
        为(行= 0;&行下,则为a.length;排++)
        {
            为(countsRow = 0; countsRow&下;则为a.length; countsRow ++)
            {            }
            的System.out.println(行+(行+ 1));
        }
    }
    静态无效printRows(一个烧焦[] [],诠释计数[] [])
    {    }    静态无效doColumns(一个烧焦[] [])
    {
        INT计数[] [];
        计数=新INT [2] [2];
        getColumns(一,计数);
        printColumns(一,计数);
    }    静态无效getColumns(一个烧焦[] [],诠释计数[] [])
    {
        INT关口,countsCol;
        为(countsCol = 0; countsCol&下;则为a.length; countsCol ++)
        {        }
    }    静态无效printColumns(一个烧焦[] [],诠释计数[] [])
    {    }    静态无效doDiagonals(一个烧焦[] [])
    {
        INT计数[] [];
        计数=新INT [2] [2];
        getDiagonals(一,计数);
        printDiagonals(一,计数);    }    静态无效getDiagonals(一个烧焦[] [],诠释计数[] [])
    {    }    静态无效printDiagonals(一个烧焦[] [],诠释计数[] [])
    {    }
}


解决方案

试试这个行数和适应他人:

 的for(int i = 0; I<大小;我++){
    INT countX = 0;
    INT countO = 0;
    对于(INT J = 0; J<大小; J ++){
           如果(阵列[I] [J] =='X')
            countX ++;
        其他
            countO ++;
    }
    的System.out.println(行:+ I ++ countX +X+ countO +O);
}

I have an assignment to create a tic tac toe board and filling the board with X, O or a space using Random. After the array is filled I have to use a method to count each X or O in each row of the method, Ex. Row 1 has 2 X's and 1 O. I have already filled the array but I don't know how to use a for loop to count the amount of X's or O's in each row of the array. After I do rows I also have to do columns and Diagonals so ignore those methods for now, Thanks

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 [][])
    {

    }
}

解决方案

Try this for rows and adapt to the others:

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");
}

这篇关于计数和从阵列打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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