Java 尝试以数独风格打印2d数组

Java 尝试以数独风格打印2d数组,java,multidimensional-array,printing,sudoku,Java,Multidimensional Array,Printing,Sudoku,所以我尝试为一个数独类做一个show()方法。它使用(9x9)2d阵列。这个方法显示以数独风格打印数组,但我不确定如何实现它。我真的很感谢你的帮助 我已经尝试过一些“for循环”,但正如我所说,我真的不知道如何在3x3正方形中分离阵列。我包含了代码的一小部分 公开展览( 我需要的输出可能是这样的 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

所以我尝试为一个数独类做一个show()方法。它使用(9x9)2d阵列。这个方法显示以数独风格打印数组,但我不确定如何实现它。我真的很感谢你的帮助

我已经尝试过一些“for循环”,但正如我所说,我真的不知道如何在3x3正方形中分离阵列。我包含了代码的一小部分

公开展览(

我需要的输出可能是这样的

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0


0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0


0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

电流输出:

0859300
50432800
03007090
0 4 5 1 0 0 0 0
27800916
0 0 0 8 4 2 0
03060020
01093607

0 0 2 7 8 5 0 0 0

如果每行中只缺少垂直行,则可以在该行中添加条件打印语句,以便在第二个和第六个元素后添加垂直行。 可能是以下情况:

if(rowIndex == 2 || rowIndex == 5) {
    System.out.print("|");
}
for(int[]row:values)
    {
        for(int rowIndex = 0; rowIndex < row.length(); rowIndex++)
        {
            System.out.print(row[rowIndex]);
            System.out.print("\t");

            if(rowIndex == 2 || rowIndex == 5) {
                 System.out.print("|");
                 System.out.print("\t");
            }
        }
        System.out.println();
    }
}
编辑:需要提到的一点是,您需要更改循环以跟踪您所在的索引

请尝试以下操作:

if(rowIndex == 2 || rowIndex == 5) {
    System.out.print("|");
}
for(int[]row:values)
    {
        for(int rowIndex = 0; rowIndex < row.length(); rowIndex++)
        {
            System.out.print(row[rowIndex]);
            System.out.print("\t");

            if(rowIndex == 2 || rowIndex == 5) {
                 System.out.print("|");
                 System.out.print("\t");
            }
        }
        System.out.println();
    }
}
for(int[]行:值)
{
对于(int-rowIndex=0;rowIndex
如果每行中只缺少垂直线,可以在该行中添加条件打印语句,因此在第二个和第六个元素之后添加垂直线。 可能是以下情况:

if(rowIndex == 2 || rowIndex == 5) {
    System.out.print("|");
}
for(int[]row:values)
    {
        for(int rowIndex = 0; rowIndex < row.length(); rowIndex++)
        {
            System.out.print(row[rowIndex]);
            System.out.print("\t");

            if(rowIndex == 2 || rowIndex == 5) {
                 System.out.print("|");
                 System.out.print("\t");
            }
        }
        System.out.println();
    }
}
编辑:需要提到的一点是,您需要更改循环以跟踪您所在的索引

请尝试以下操作:

if(rowIndex == 2 || rowIndex == 5) {
    System.out.print("|");
}
for(int[]row:values)
    {
        for(int rowIndex = 0; rowIndex < row.length(); rowIndex++)
        {
            System.out.print(row[rowIndex]);
            System.out.print("\t");

            if(rowIndex == 2 || rowIndex == 5) {
                 System.out.print("|");
                 System.out.print("\t");
            }
        }
        System.out.println();
    }
}
for(int[]行:值)
{
对于(int-rowIndex=0;rowIndex
公共作废显示()
{ 
对于(int x=0;x<9;x++)
{
对于(int y=0;y<9;y++)
{
系统输出打印(值[x][y]);
系统输出打印(“\t”);
如果((y+1)%3==0){
系统输出打印(“|\t”);
}
}
System.out.println();
如果((x+1)%3==0){
System.out.println(“-------------------------”);
}
}
}
公共作废显示()
{ 
对于(int x=0;x<9;x++)
{
对于(int y=0;y<9;y++)
{
系统输出打印(值[x][y]);
系统输出打印(“\t”);
如果((y+1)%3==0){
系统输出打印(“|\t”);
}
}
System.out.println();
如果((x+1)%3==0){
System.out.println(“-------------------------”);
}
}
}

您当前打印的输出是什么样子的?它只是缺少垂直线吗?很抱歉,我不知道如何确切地解释我需要的输出,我实际上不需要这么多的空间。我只需要用|和-,来分隔3x3块。请看这个答案@Matthew,它缺少两行。提示:从使用有意义的变量名开始。a、 n,价值观。。。不要告诉人类读者。为什么不行、列。。。等等?您当前打印的输出是什么样子的?它只是缺少垂直线吗?很抱歉,我不知道如何确切地解释我需要的输出,我实际上不需要这么多的空间。我只需要用|和-,来分隔3x3块。请看这个答案@Matthew,它缺少两行。提示:从使用有意义的变量名开始。a、 n,价值观。。。不要告诉人类读者。为什么不行、列。。。以此类推?+1期待您添加如何以相同方式添加水平线(从连字符、endash、emdash中选择最合适的)。+1期待您添加如何以相同方式添加水平线(从连字符、endash、emdash中选择最合适的)。