Java 如何为战舰游戏的网格阵列编号?

Java 如何为战舰游戏的网格阵列编号?,java,Java,你好,我正试图在我的战列舰“海洋”号的顶部和左侧执行0-9的数字。我希望它看起来像这样 0 1 2 3 4 5 6 7 8 9 +--+--+--+--+--+--+--+--+--+--+ 0| | | | | | | | | | | +--+--+--+--+--+--+--+--+--+--+ 1| | | | | | | | | | | +--+--+--+--+--+--+--+--+--+--+ 2| (yo

你好,我正试图在我的战列舰“海洋”号的顶部和左侧执行0-9的数字。我希望它看起来像这样

  0  1  2  3  4  5  6  7  8  9 
 +--+--+--+--+--+--+--+--+--+--+
0|  |  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--+--+  
1|  |  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--+--+
2|
 (you get the idea!)
3
4
5
6
7
8
9
我已经尝试了很多if循环,但没有真正的结果。我可以显示数字,但不是我想要的方式

public void printOcean(boolean showShips) {
    for (int row = 0; row < OCEAN_SIZE; row++) 
    {
        if(row == 0)
        {
            System.out.print(" ");
            for (int i = 0; i < OCEAN_SIZE; i++) 
            {
                System.out.print(String.format("%4s", i));
                System.out.println();
                {
                    System.out.print(" ");
                    System.out.print("+---");        
                }
                System.out.print("+\n");
                System.out.print(row +" ");


                for (int col = 0; col < OCEAN_SIZE; col++) 
                {
                    System.out.print("| " + ocean[row][col].printCoordinate(showShips) + " ");
                }
                System.out.print("|");
                System.out.println(); 
            }
            System.out.print(" ");
        }
        for (int i = 0; i < OCEAN_SIZE; i++) 
        {
            System.out.print("+---");           
        }
        System.out.print("+\n");
    }
}
然而,我得到的结果是

    0
 +---+
0 |
   1
 +---+
0 |
   2
 +---+
0 |
   3
 +---+
0 |
   4
 +---+
0 |
   5
 +---+
0 |
   6
 +---+
0 |
   7
 +---+
0 |
   8
 +---+
0 |
 +---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+

试试这个。但是你应该在某个地方定制它

public class Main {
    static int OCEAN_SIZE = 10;

    public static void main(String[] args) {
        printOcean(false);
    }

    private static void printColumnIndexes() {
        System.out.print("  ");
        for (int i = 0; i < OCEAN_SIZE; i++) {
            System.out.print(" " + i + " ");
        }
        System.out.println(" ");
    }

    private static void printBorders() {
        System.out.print(" ");
        for (int j = 0; j < OCEAN_SIZE; j++) {
            System.out.print("+--");
        }
        System.out.println();
    }

    private static void printRows(boolean showShips) {
        for (int i = 0; i < OCEAN_SIZE; i++) {
            // printing row indexes
            System.out.print(i + "|");

            // printing main body
            for (int j = 0; j < OCEAN_SIZE; j++) {
                // Here you can put your ships
                if (showShips) {
                    // print your ship
                    // from your ship map[i][j].
                    // mention that cell size from your 
                    // sample is 2. So it should be something
                    // like this: "* " or " *" etc.
                    // if (map[i][j]) {
                    //     System.out.print("* |");
                    // } else {
                    //     System.out.print("  |");
                    // }
                } else {
                    System.out.print("  |");
                }

            }
            System.out.println();

            // printing extra borders after row filled
            printBorders();
        }
    }

    private static void printOcean(boolean showShips) {
        printColumnIndexes();
        printBorders();
        printRows(showShips);
    }
}

因此,您可以自定义
printRows()
方法来填充您的飞船。

对于人类,行和列通常以1开头,很简单,您的循环是无序的。将最后一个循环切换到主循环,而不是之后再进行。-我只是把你的代码放到一个文本编辑器中,你也需要修正你的缩进和括号。例如,您无缘无故地拥有一个额外的范围大括号,您可能会发现将每种“类型”的行作为一个单独的方法分开是有帮助的。一种方法是打印列标题,一种方法是打印一行水平网格(
+--+…
),另一种方法是打印垂直条和单元格间距(可能还需要打印战列舰和其他东西)。让这些方法单独工作,你会发现打印你想要的整体结构要容易得多。给你的提示是,把你的代码分解成不同的函数。-我建议处理内容的
drawCell(x,y)
drawCellLine()
绘制编号的线条(调用
drawCell()
作为其一部分),并
drawInterCellLine()
绘制
+-+
模式。然后你只需要有一个父函数来依次调用它们。谢谢Baldrickk,看起来我有一些工作要做!我仍然会添加一个新函数来打印单元格内容,而不是直接自定义
printRows
(虽然它应该调用该新函数),从原始示例来看,这里的大小似乎是10。@Baldrickk,你说得对。我只是给出一些模板。所以,用户应该添加额外的方法来打印单元格内容。最好是推导逻辑。
public class Main {
    static int OCEAN_SIZE = 10;

    public static void main(String[] args) {
        printOcean(false);
    }

    private static void printColumnIndexes() {
        System.out.print("  ");
        for (int i = 0; i < OCEAN_SIZE; i++) {
            System.out.print(" " + i + " ");
        }
        System.out.println(" ");
    }

    private static void printBorders() {
        System.out.print(" ");
        for (int j = 0; j < OCEAN_SIZE; j++) {
            System.out.print("+--");
        }
        System.out.println();
    }

    private static void printRows(boolean showShips) {
        for (int i = 0; i < OCEAN_SIZE; i++) {
            // printing row indexes
            System.out.print(i + "|");

            // printing main body
            for (int j = 0; j < OCEAN_SIZE; j++) {
                // Here you can put your ships
                if (showShips) {
                    // print your ship
                    // from your ship map[i][j].
                    // mention that cell size from your 
                    // sample is 2. So it should be something
                    // like this: "* " or " *" etc.
                    // if (map[i][j]) {
                    //     System.out.print("* |");
                    // } else {
                    //     System.out.print("  |");
                    // }
                } else {
                    System.out.print("  |");
                }

            }
            System.out.println();

            // printing extra borders after row filled
            printBorders();
        }
    }

    private static void printOcean(boolean showShips) {
        printColumnIndexes();
        printBorders();
        printRows(showShips);
    }
}
   0  1  2  3  4  5  6  7  8  
 +--+--+--+--+--+--+--+--+--
0|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
1|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
2|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
3|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
4|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
5|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
6|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
7|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--
8|  |  |  |  |  |  |  |  |  |
 +--+--+--+--+--+--+--+--+--