如何在嵌套的二维数组循环中更改或插入值

huangapple go评论67阅读模式
英文:

How to change or insert value in a 2D Array nested for loop

问题

以下是翻译好的部分:

public void printMap() {
    map = new int[15][30];

    System.out.println();
    for (int i = 0; i < map.length; i++){
        System.out.print("|");
        for (int k = 0; k < map[i].length; k++){
            if (i == 0) {
                System.out.print("-");
            } else if (i == map.length - 1) {
                System.out.print("_");
            } else if (k == -5) { // 我想从另一个方法中注入 -5 到网格中,以便在注入 -5 的位置打印 '^'
                System.out.print("^");
            } else {
                System.out.print(" ");
            }
        }
        System.out.println("|");
    }
}

请注意,这段代码似乎有一些逻辑错误,例如在循环条件 k == -5 中。另外,此处没有提供完整的上下文,可能会影响最终的理解和修复。如果您需要更多帮助,请随时提供更多信息。

英文:

I'm am very new to programming and working just in the CLI. I may be trying to do something completely wrong or going about it the wrong way. I am using a nested for loop to create a 2d grid array, assigning characters to my (int i) and (int k) to create a border. now in another method am trying to inject a value to the grid so when the grid is printed I can change the char in that position of the grid. I have done this manually building the gird as you can just select grid[1][2] = a. just can't seem to wrap my head around it using the for a loop. reason for using the for a loop as I can adjust the size of the grid and display the border by just adjusting grid size.

public void printMap() {
        map = new int[15][30];

        System.out.println();
        for (int i = 0; i &lt; map.length; i++){
                System.out.print(&quot;|&quot;);
                for (int k = 0; k &lt; map[i].length; k++){
                    if (i == 0) {
                        System.out.print(&quot;-&quot;);
                    }else if (i == map.length-1){
                        System.out.print(&quot;_&quot;);
                    }else if (k == -5){ // i want to inject -5 from another method into the grid so it prints out &#39;^&#39; where ever i inject -5
                        System.out.print(&quot;^&quot;);
                    }else {
                        System.out.print(&quot; &quot;);

                }
            }
            System.out.println(&quot;|&quot;);
        }

    }

答案1

得分: 2

我认为你应该在网格上拆分为两个操作:updateprint。最好将网格的定义更改为 char[][],并且 print() 方法只打印此内容(不需要考虑不同字符可能表示的内容)。如果你想要保存 int,只需使用 if...else 来打印正确的字符。

此外,你应该将网格的定义与内容分开。看看我的简单示例。有几个操作:

  • 在网格上添加边框
  • 打印网格
  • 在网格上添加笑脸图像
  • 在网格上添加十字形图像

private static void addBorder(char[][] grid) {
    for (int row = 0; row < grid.length; row++) {
        for (int col = 0; col < grid[row].length; col++) {
            if (col == 0 || col == grid[row].length - 1)
                grid[row][col] = '|';
            else if (row == 0 || row == grid.length - 1)
                grid[row][col] = '-';
            else
                grid[row][col] = ' ';
        }
    }
}

private static void showSmile(char[][] grid) {
    clearGrid(grid);
    // 仅适用于 5x5 的网格
    grid[1][1] = '^';
    grid[1][3] = '^';
    grid[2][2] = '|';
    grid[3][2] = '-';
}

private static void showCross(char[][] grid) {
    clearGrid(grid);
    // 仅适用于 5x5 的网格
    grid[1][2] = '|';
    grid[2][2] = '|';
    grid[3][2] = '|';
    grid[2][1] = '-';
    grid[2][3] = '-';
}

private static void clearGrid(char[][] grid) {
    for (int row = 1; row < grid.length - 1; row++)
        for (int col = 1; col < grid[row].length - 1; col++)
            grid[row][col] = ' ';
}

private static void printGrid(char[][] grid) {
    for (int row = 0; row < grid.length; row++) {
        for (int col = 0; col < grid[row].length; col++)
            System.out.print(grid[row][col]);

        System.out.println();
    }
}

// 输出:

public static void main(String[] args) {
    char[][] grid = new char[5][5];
    addBorder(grid);
    printGrid(grid);
    System.out.println();

    showSmile(grid);
    printGrid(grid);
    System.out.println();

    showCross(grid);
    printGrid(grid);
}

|---|
|   |
|   |
|   |
|---|

|---|
|^ ^|
| | |
| - |
|---|

|---|
| | |
|-|-|
| | |
|---|
英文:

I think you should split two operation on the grid: update and print. It's better to change grid definition to char[][] and print() method just prints this content (it should not care about what different characters could mean). In case you want to hold int instead, so just use if...else to print correct character.

Additionally, you should split definition of the grid and it content. Look at my simple example. There're several operations:

  • add border to the grid
  • print the grid
  • add smile image to the grid
  • add cross image to the grid

private static void addBorder(char[][] grid) {
for (int row = 0; row &lt; grid.length; row++) {
for (int col = 0; col &lt; grid[row].length; col++) {
if (col == 0 || col == grid[row].length - 1)
grid[row][col] = &#39;|&#39;;
else if (row == 0 || row == grid.length - 1)
grid[row][col] = &#39;-&#39;;
else
grid[row][col] = &#39; &#39;;
}
}
}
private static void showSmile(char[][] grid) {
clearGrid(grid);
// only for 5x5 grid
grid[1][1] = &#39;^&#39;;
grid[1][3] = &#39;^&#39;;
grid[2][2] = &#39;|&#39;;
grid[3][2] = &#39;-&#39;;
}
private static void showCross(char[][] grid) {
clearGrid(grid);
// only for 5x5 grid
grid[1][2] = &#39;|&#39;;
grid[2][2] = &#39;|&#39;;
grid[3][2] = &#39;|&#39;;
grid[2][1] = &#39;-&#39;;
grid[2][3] = &#39;-&#39;;
}
private static void clearGrid(char[][] grid) {
for (int row = 1; row &lt; grid.length - 1; row++)
for (int col = 1; col &lt; grid[row].length - 1; col++)
grid[row][col] = &#39; &#39;;
}
private static void printGrid(char[][] grid) {
for (int row = 0; row &lt; grid.length; row++) {
for (int col = 0; col &lt; grid[row].length; col++)
System.out.print(grid[row][col]);
System.out.println();
}
}

Output:

public static void main(String[] args) {
char[][] grid = new char[5][5];
addBorder(grid);
printGrid(grid);
System.out.println();
showSmile(grid);
printGrid(grid);
System.out.println();
showCross(grid);
printGrid(grid);
}
|---|
|   |
|   |
|   |
|---|
|---|
|^ ^|
| | |
| - |
|---|
|---|
| | |
|-|-|
| | |
|---|

答案2

得分: 0

以下是您要求的翻译内容:

private void displayGame(){
    printMap(2, 2);
}

public void printMap(int playerPositionX, int playerPositionY) {
    System.out.println();
    boolean foundRow = false;
    boolean foundPosition = false;
    for (int i = 0; i < map.length; i++){
        System.out.print("|");
        if (!foundRow && playerPositionY == i){
            foundRow = true;
        }
        for (int k = 0; k < map[i].length; k++){
            if (foundRow && (playerPositionX == k)){
                foundPosition = true;
            }
            if (i == 0) {
                System.out.print("-");
            } else if (i == map.length-1){
                System.out.print("_");
            } else if (foundPosition){
                System.out.print("^");
                foundPosition = false;
                foundRow = false;
            } else {
                System.out.print(" ");
            }
        }
        System.out.println("|");
    }
}
英文:

talking with a friend he gave me this solution. so now at int 'i' (2) and int 'k'(2) it will print '^'

private void displayGame(){
printMap(2,2);
}
public void printMap(int playerPositionX, int playerPositionY) {
System.out.println();
boolean foundRow = false;
boolean foundPosition = false;
for (int i = 0; i &lt; map.length; i++){
System.out.print(&quot;|&quot;);
if (!foundRow &amp;&amp; playerPositionY == i){
foundRow = true;
}
for (int k = 0; k &lt; map[i].length; k++){
if (foundRow &amp;&amp; (playerPositionX == k)){
foundPosition = true;
}
if (i == 0) {
System.out.print(&quot;-&quot;);
}else if (i == map.length-1){
System.out.print(&quot;_&quot;);
}else if (foundPosition){
System.out.print(&quot;^&quot;);
foundPosition = false;
foundRow = false;
}else {
System.out.print(&quot; &quot;);
}
}
System.out.println(&quot;|&quot;);
}
}
</details>

huangapple
  • 本文由 发表于 2020年9月21日 08:12:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63984774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定