【问题标题】:Having x,y coordinates from 0-9 on a 2d dimensional array在二维数组上具有 0-9 的 x,y 坐标
【发布时间】:2023-04-01 14:42:01
【问题描述】:

这是我的棋盘游戏代码,但是我无法添加我的 x,y 坐标 0-9。有人可以帮忙吗?

String[][] board = new String [10][10];
for (String[] board1 : board) {
    for (int c = 0; c <board.length; c++) {
        board1[c] = ".";
    }
}

for (String[] board1 : board) {
    for (int c = 0; c <board.length; c++) {
        System.out.print(board1[c] + " ");
    }
    System.out.println();

【问题讨论】:

  • 嘿,莎拉,我们需要查看您的(相关)代码才能提供帮助。此外,一个更具体的问题会有所帮助。
  • 没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建Minimal, Complete, and Verifiable example
  • Hiya Domdom,这是我的板代码,但是我需要 0-9 出现在我正在努力的 x、y 角上。字符串[][]板=新字符串[10][10]; for (String[] board1 : board) { for (int c = 0; c

标签: java arrays multidimensional-array


【解决方案1】:
    String[][] board = new String [9][9]; 
    for (String[] board1 : board) 
    { 
        for (int c = 0; c <board.length; c++) 
        { 
            board1[c] = "."; 
        } 
    }

    System.out.print("0");
    for(int i = 1; i < 10; i++)
        System.out.print(" " + i);
    System.out.println();
    int i = 1;
    for (String[] board1 : board) 
    {
        System.out.print((i++) + " ");
        for (int c = 0; c <board.length; c++) 
        { 
            System.out.print(board1[c] + " "); 
        } 
        System.out.println();
    }

只需稍微玩一下代码,您就可以得到它。练习做一些小改动并运行代码,重复一遍。

【讨论】:

    【解决方案2】:

    以后,您应该尝试提出一个非常具体的问题,并在您的问题中显示您尝试过的代码。见How to Create a Minimal, Complete, and Verifiable Example

    据我了解,这是您当前的代码:

    String[][] board = new String[10][10];
    for(String[] board1 : board){
        for(int c=0; c<board.length; c++){
            System.out.println(board1[c] + " ");
        }
    }
    System.out.println();
    

    这是你想要输出的:

    0 1 2 3 4 5 6 7 8 9
    0 - - - - - - - - -
    2 - - - - - - - - -
    3 - - - - - - - - -
    4 - - - - - - - - -
    5 - - - - - - - - -
    6 - - - - - - - - - 
    7 - - - - - - - - -
    8 - - - - - - - - -
    9 - - - - - - - - - 
    

    你的问题应该是这样问的。

    对于答案,您必须将程序视为具有两个步骤。第一步是设置数组的值,第二步是打印出来。

    第一步,只需使用嵌套在另一个循环中的循环:

        for(int i=0; i<board.length; i++){
            for(int j=0; j<board[0].length; j++){
                if(i==0){ //if the cell is in the first row, set it to the column number
                    board[i][j] = Integer.toString(j);
                } else if(j==0){ //if the cell is in the first column, set it to the row number
                    board[i][j] = Integer.toString(i);
                } else{
                    board[i][j] = "-";
                }
            }
        }
    

    现在值已设置,您必须打印它们。把它想象成打印每一行,一次一个。在每一行中,您一次打印一个列值。

        for(int row=0; row<board.length; row++){
            for(int column=0; column<board[0].length; column++){
                System.out.print(board[row][column] + " "); //after each value, add a space
            }
            System.out.println(); //at the end of each row, go to the next line
        }
    

    请注意,此代码在每行末尾添加了一个额外的空格。

    【讨论】:

      猜你喜欢
      • 2020-11-23
      • 1970-01-01
      • 2016-04-28
      • 2021-12-31
      • 2012-06-02
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 2016-08-02
      相关资源
      最近更新 更多