我有这段C#代码,需要在Java中重新编写。

private static void ShowGrid(CellCondition[,] currentCondition)
{

        int x = 0;
        int rowLength =5;

        foreach (var condition in currentCondition)
        {
            var output = condition == CellCondition.Alive ? "O" : "·";
            Console.Write(output);
            x++;
           if (x >= rowLength)
            {
                x = 0;
                Console.WriteLine();
            }
        }
}


到目前为止,我的Java代码如下所示:

private static void ShowGrid(CellCondition[][] currentCondition) {

        int x = 0;
        int rowLength = 5;

        for(int i=0;i<currentCondition.length;i++){
            for(int j =0; j<currentCondition[0].length;j++){
                CellCondition[][] condition = currentCondition[i][j];
                //I am stuck at here
                x++;
            if(x>=rowLength){
               x=0;
               System.out.println();
               }
            }
        }
}


我被困在CellCondition[][] condition = currentCondition[i][j];行之后,我不确定循环是否正确完成。任何建议将不胜感激。

最佳答案

private static void ShowGrid(CellCondition[][] currentCondition) {
    int x = 0;
    int rowLength = 5;

    for(int i = 0; i < currentCondition.length; i++) {
        for(int j = 0; j < currentCondition[0].length; j++) {
            CellCondition condition = currentCondition[i][j];
            String output = (condition == CellCondition.Alive ? "O" : "·");
            System.out.print(output);
            x++;
            if(x >= rowLength) {
               x = 0;
               System.out.println();
            }
        }
    }
}


只需访问单元。每个单元格是一个CellCondition,而不是一个CellCondition[][]

10-05 18:20