我该如何使用“或”在 Java 的 if 语句中而不需要重新输入整个表达式?

发布于 2024-10-17 01:15:41 字数 557 浏览 4 评论 0原文

我正在为类的扫雷项目编写代码,一种方法是 numAdjMines,它计算数组中某个单元格周围的地雷,每种类型的单元格都有不同的值,例如地雷为 -2,而地雷上带有标志是-4。我只想编写一个 if 语句,但最终不得不编写相同的代码两次,最后使用不同的值。

if (row >= 1 && col >= 1 && boardArray[row - 1][col - 1] == MINE)
    {
        adjMines = adjMines + 1;
    }
if (row >= 1 && col >= 1 &&
            boardArray[row - 1][col - 1] == FLAGGED_MINE)
    {
        adjMines = adjMines + 1;
    }

我尝试使用||或 和 写作 || boardArray[row-1][col-1] == FLAGGED_MINE 在第一个的末尾,但随后忽略了检查行和列的开头。有没有一种简短紧凑的方法可以让我编写这段代码?

I'm writing code for a minesweeper project for class and one method is numAdjMines, which counts the mines around a cell in the array, each type of cell has a different value, like mines are -2, while mines with a flag on them are -4. I want to just write one if statement, but I end up having to just write the same code twice, with different values at the end.

if (row >= 1 && col >= 1 && boardArray[row - 1][col - 1] == MINE)
    {
        adjMines = adjMines + 1;
    }
if (row >= 1 && col >= 1 &&
            boardArray[row - 1][col - 1] == FLAGGED_MINE)
    {
        adjMines = adjMines + 1;
    }

I tried using || for or and writing || boardArray[row-1][col-1] == FLAGGED_MINE at the end of the first one, but that then ignored the beginning with checking the row and column. Is there a short compact way for me to write this code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

停滞 2024-10-24 01:15:41

您的上述代码实际上可以压缩为单个 IF 语句,但是我认为您的实际代码包含更多语句,否则您已经这样做了。

简化此类代码的最简单方法是将其分成两层 IF 语句。外层是一般情况,内层是特殊情况。

if (row >= 1 && col >= 1 ){

    int cell = boardArray[row - 1][col - 1];

    if( cell == MINE ){
        // Code here
    }

    else if( cell == FLAGGED_MINE )
    {
        // Code here
    }

}

Your above code can actually be compressed into a single IF statement, however I presume your actual code contains more statements otherwise you would have done this already.

The easiest way to simplify such code would be to break it into two layers of IF statements. The outer one contains the common condition, and the inner ones contain the specific conditions.

if (row >= 1 && col >= 1 ){

    int cell = boardArray[row - 1][col - 1];

    if( cell == MINE ){
        // Code here
    }

    else if( cell == FLAGGED_MINE )
    {
        // Code here
    }

}
青衫负雪 2024-10-24 01:15:41

为了避免重复,您可以使用嵌套的 if 语句,即两个条件都依赖于 row & col >= 1,所以将其放入它自己的语句中。

然后我猜你想避免多次从数组中取出值,所以最好的办法是将其分配给一个变量。这在运行时可能不会更有效,但是看起来更好。

if (row >= 1 && col >= 1)
{
    int value = boardArray[row - 1][col - 1];
    if (value == MINE || value == FLAGGED_MINE)
    {
        adjMines = adjMines + 1;
    }
}

To avoid repetition you can use nested if statements, ie both conditions rely on row & col being >= 1, so pull that out into it's own statement.

Then i'm guessing you want to avoid pulling the value out of the array multiple times, so the best thing to do is assign it to a variable. this probably isn't more efficient at runtime, however is nicer to look at.

if (row >= 1 && col >= 1)
{
    int value = boardArray[row - 1][col - 1];
    if (value == MINE || value == FLAGGED_MINE)
    {
        adjMines = adjMines + 1;
    }
}
预谋 2024-10-24 01:15:41

您可以使用括号对条件进行分组

if(row >= 1 && col >= 1 && 
  (boardArray[row - 1][col - 1] == MINE
    || boardArray[row - 1][col - 1] == FLAGGED_MINE))
{
    adjMines = adjMines + 1;
}

You can use parentheses to group the conditions

if(row >= 1 && col >= 1 && 
  (boardArray[row - 1][col - 1] == MINE
    || boardArray[row - 1][col - 1] == FLAGGED_MINE))
{
    adjMines = adjMines + 1;
}
乖乖 2024-10-24 01:15:41

我不确定您要避免重复哪一部分,但您可以通过嵌套 if 语句来避免第一部分的重复。

if( row >= 1 && col >= 1 ) {
    if( boardArray[row - 1][col - 1] == MINE || 
        boardArray[row - 1][col - 1] == FLAGGED_MINE ) {

        adjMines = adjMines + 1;
    }
}

我会更进一步,将内部 if 语句设置为方法调用。

I'm not sure which part you're trying to avoid repeating, but you can avoid the repetion on the first part by nesting the if statements.

if( row >= 1 && col >= 1 ) {
    if( boardArray[row - 1][col - 1] == MINE || 
        boardArray[row - 1][col - 1] == FLAGGED_MINE ) {

        adjMines = adjMines + 1;
    }
}

I'd take it even one step further and make the inner if statement a method call.

冬天的雪花 2024-10-24 01:15:41

我不确定我是否完全理解这个问题,但以下代码更紧凑并实现相同的目标:

if (row >= 1 && col >= 1 && 
    (boardArray[row - 1][col - 1] == MINE || boardArray[row - 1][col - 1] == FLAGGED_MINE)) {
    adjMines++;
}

我不确定写入主数组对于标记的地雷来说是否是一个好主意。我想玩家可能会错误地标记一个空闲单元格,这会将单元格标记为已标记的地雷,而您将不知道地雷是否确实存在。也许您有处理此问题的逻辑,但最好保留原始板(数据结构)以供将来参考。

I am not sure if I entirely understand the question, but the following code is more compact and accomplish the same goal:

if (row >= 1 && col >= 1 && 
    (boardArray[row - 1][col - 1] == MINE || boardArray[row - 1][col - 1] == FLAGGED_MINE)) {
    adjMines++;
}

I am not sure if writing to your main array is a good idea for the flagged mines. I suppose a player can flag a free cell by mistake and that would mark the cell as a flagged mine and you won't know if a mine was actually there. Maybe you have logic for handling this, but it's always good to have your original board (the data structure) safe for future reference.

貪欢 2024-10-24 01:15:41
for(    int r : array( row-1, row, row+1 ))
    for(int c : array( col-1, col, col+1 ))
    {
        if(r==row && c==col) // center
            continue; 

        if(0<=r&&r<ROWS && 0<=c&&c<COLS) // within bounds
        {
            int state = boardArray[r][c];
            if(state==MINE||state==FLAGGED_MINE)
                adjMines++;
        }
     }

int[] array(int... ints){ return ints; }

所以你不必编写 8 个单独的案例。这没什么大不了的,但如果你有一个 3D 地雷......

for(    int r : array( row-1, row, row+1 ))
    for(int c : array( col-1, col, col+1 ))
    {
        if(r==row && c==col) // center
            continue; 

        if(0<=r&&r<ROWS && 0<=c&&c<COLS) // within bounds
        {
            int state = boardArray[r][c];
            if(state==MINE||state==FLAGGED_MINE)
                adjMines++;
        }
     }

int[] array(int... ints){ return ints; }

So you don't have to write 8 seperate cases. Not a very big deal here, but if you have a 3D mine...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文