Java 在矩阵中显示隐藏位置

Java 在矩阵中显示隐藏位置,java,multidimensional-array,Java,Multidimensional Array,我正试图编写一个程序来隐藏/显示矩阵的某些部分。这很难解释,但这里有一个直观的例子 1 * * * * 2 * * * * 3 * * * * 4 * * * * 1 2 3 4 如果用户输入1,4,则位置1,4的星号将替换为隐藏在其后面的值:我的数组中的0,3 1 * * * A 2 * * * * 3 * * * * 4 * * * * 1 2 3 4 到目前为止,我能够为第一个没有任何显示字符的网格生成代码。关于如何让我的程序显示用户输入的部件,有什么提

我正试图编写一个程序来隐藏/显示矩阵的某些部分。这很难解释,但这里有一个直观的例子

1 * * * * 
2 * * * * 
3 * * * * 
4 * * * * 
  1 2 3 4  
如果用户输入1,4,则位置1,4的星号将替换为隐藏在其后面的值:我的数组中的0,3

1 * * * A 
2 * * * * 
3 * * * * 
4 * * * * 
  1 2 3 4  
到目前为止,我能够为第一个没有任何显示字符的网格生成代码。关于如何让我的程序显示用户输入的部件,有什么提示吗

以下是我目前的代码:

for(int i = 1; i <= board.length;i++){
System.out.print(i+ " ");
    for(int j = 0; j < board.length;j++){
        System.out.print("* ");
    }
    System.out.println("");
}
System.out.print("  ");
for(int t=1; t<= board.length;t++){
    System.out.print(t+" ");
}
System.out.println(" ");

System.out.println("Enter a pair of undiscovered distinct locations on the board that you want           revealed.");

int userinput1 = keyboard.nextInt();
int userinput2 = keyboard.nextInt();


while(userinput1 > board.length || userinput2 > board.length){
    System.out.println("The location is invalid. It is outside of the board.");

    System.out.println("Re enter your first location:");

    userinput1 = keyboard.nextInt();
    userinput2 = keyboard.nextInt();
}

// here I need to redisplay the board with the location revealed

这是打印星星阵列的方式,下面没有标签:

for(int i = 1; i <= board.length;i++){
System.out.print(i+ " ");
    for(int j = 0; j < board.length;j++){
        System.out.print("* ");
    }
    System.out.println("");
}
您希望显示网格中的特定位置,而不是整个位置。因此,我们需要一种方法来确定是否披露该位置。一种可能是使用布尔值[]。这意味着你应该这样做:

for(int i = 1; i <= board.length; i++) {
    System.out.print(i +  " ");
    for(int j = 0; j < board[i].length; j++) {
        if (isrevealed(i,j)) System.out.print(board[i][j] + " ");
        else System.out.print("* ");
    }
    System.out.println();
}
请注意,您可能应该将此代码包装在一个方法中,因为您似乎将多次使用它。示例方法:

public static void printBoard(char[][] board, boolean[][] is_revealed) {
    for(int i = 1; i <= board.length; i++) {
        System.out.print(i +  " ");
        for(int j = 0; j < board[i].length; j++) {
            if (is_revealed[i][j]) System.out.print(board[i][j] + " ");
            else System.out.print("* ");
        }
        System.out.println();
    }
}

我应该加上吗?虽然这段代码很长,但这部分并不是代码的真正目的,所以添加整个代码会让人感到困惑。欢迎使用SOF。我们在这里回答有关代码的问题,但不是为您编写代码。它通常是这样工作的:编写代码后遇到问题。粘贴您怀疑导致问题的代码部分,以及出错原因的描述。在此之后,这里的某个人将有望帮助您解决问题。为什么不通过放置精确的行和列索引来重新初始化数组呢。e、 g.数组[userRow-1][userColumn-1]=userValue@VighaneshGursale当我们甚至不知道代码是什么的时候,你怎么能这么说?我可以想出imsocool显示数组的其他几种方法。我在放置代码时遇到问题。。。我可以发布java文件吗?非常感谢您,我还没有弄明白,但我很抱歉,我想我快得到它了。@imsocoolhahahaha不客气。将来,试着让你的问题看起来像现在一样。还要注意的是,如果你能展示出你所做的事情是为了真正做到你所要求的,即使那不起作用,那也会更好。我还有另一个问题。我有一封信要透露,但如果我想要两封呢?@imsocoolhahahaha你可能使用了一个简单的布尔变量。我们希望每个可能的位置都有一个布尔变量,所以我们需要一个大小合适的布尔[][],不过这是个愚蠢的问题。刚刚添加了一个else if,并为第二组值添加了另一个布尔值。那太尴尬了。。。。
for(int i = 1; i <= board.length; i++){
    System.out.print(i +  " ");
    for(int j = 0; j < board[i].length; j++){
        System.out.print(display[i][j] + " ");
    }
    System.out.println();
}
public static void printBoard(char[][] board, boolean[][] is_revealed) {
    for(int i = 1; i <= board.length; i++) {
        System.out.print(i +  " ");
        for(int j = 0; j < board[i].length; j++) {
            if (is_revealed[i][j]) System.out.print(board[i][j] + " ");
            else System.out.print("* ");
        }
        System.out.println();
    }
}