java - while 循环似乎在完成循环之前重复步骤?

标签 java syntax do-while

我仍处于编写此程序的早期阶段(突破游戏),但是当尝试测试运行我已经获得的代码时,似乎在一个循环完成之前,在 do while 循环中重复了步骤。看起来它在完成循环之前运行了 obj.userMove(row, col, move); 三次。

import java.util.Scanner;
import java.util.Random;
class BreakThroughGame {
char board[][];
Random rand = new Random();


BreakThroughGame() {
    board = new char[][]{{' ','1','2','3','4','5','6','7','8', ' '},
        {'1','w','w','w','w','w','w','w','w','1'},
        {'2','w','w','w','w','w','w','w','w','2'}, 
        {'3','_', '_','_', '_','_', '_','_', '_','3'}, 
        {'4','_', '_','_', '_','_', '_','_', '_','4'}, 
        {'5','_', '_','_', '_','_', '_','_', '_','5'}, 
        {'6','_', '_','_', '_','_', '_','_', '_','6'}, 
        {'7','b', 'b','b', 'b','b', 'b','b', 'b','7'},
        {'8','b', 'b','b', 'b','b', 'b','b', 'b','8'},
        {' ','1','2','3','4','5','6','7','8',' '}};


public boolean userMove( int row, int col, String move ) {
    System.out.println("pos = "+board[row][col]);

    if (board[row][col] == 'b') {
        if(move.charAt(0) == 'f' && board[row+1][col] == 'w') {
            System.out.println("Can't move there, try again");
            return false;
        }
        switch (move.charAt(0)){
            case 'f':
                board[row-1][col] = 'b';
                board[row][col] = '_';
                return true;
            case 'r':
                board[row-1][col+1] = 'b';
                board[row][col] = '_';
                return true;
            case 'l':
                board[row-1][col-1] = 'b';
                board[row][col] = '_';
                return true;
        }
    }
    else {
        System.out.println("Invalid position, try again");
        return false;
    }
    return false;
}


public void computerMove() {
    int rm = rand.nextInt(8)+1;
    int cm = rand.nextInt(8)+1;
    int dm = rand.nextInt(2);
    //code goes here

}

public boolean gameOver() {
    //code goes here
    return false;
} 

public void printBoard() {
    for (int row = 0; row <= 9; row++){
        for (int column = 0; column <= 9; column++) {
            System.out.print(board[row][column]+" ");
        }
        System.out.println();
    }
  }
} 

public class game {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    BreakThroughGame obj = new BreakThroughGame();

    do {
        obj.printBoard();
        System.out.println("Enter row position");
        int row = in.nextInt();
        System.out.println("Enter column position");
        int col = in.nextInt();
        System.out.println("Enter move direction");
        String move = in.next();

        obj.userMove(row, col, move);
        System.out.println(obj.userMove(row, col, move));

        if(obj.userMove(row, col, move) == true)
            obj.computerMove();

    }while (obj.gameOver() == false);
  }
}

这是完成第一个循环后的输出:

  1 2 3 4 5 6 7 8   
1 w w w w w w w w 1 
2 w w w w w w w w 2
3 _ _ _ _ _ _ _ _ 3
4 _ _ _ _ _ _ _ _ 4 
5 _ _ _ _ _ _ _ _ 5 
6 _ _ _ _ _ _ _ _ 6 
7 b b b b b b b b 7 
8 b b b b b b b b 8 
  1 2 3 4 5 6 7 8   
Enter row position
7
Enter column position
5
Enter move direction
f
pos = b
pos = _
Invalid position, try again
false
pos = _
Invalid position, try again

现在循环似乎终于完成了,然后再次从顶部开始

  1 2 3 4 5 6 7 8
1 w w w w w w w w 1 
2 w w w w w w w w 2 
3 _ _ _ _ _ _ _ _ 3 
4 _ _ _ _ _ _ _ _ 4 
5 _ _ _ _ _ _ _ _ 5 
6 _ _ _ _ b _ _ _ 6 
7 b b b b _ b b b 7 
8 b b b b b b b b 8 
  1 2 3 4 5 6 7 8   
Enter row position

最佳答案

您在循环中调用该方法 3 次:

// 1
obj.userMove(row, col, move);

// 2
System.out.println(obj.userMove(row, col, move));

// 3
if(obj.userMove(row, col, move) == true)

我假设您想调用它一次并在所有 3 个地方使用相同的结果。将结果分配给一个变量,然后在这 3 个地方使用该变量。

boolean moveResult = obj.userMove(row, col, move);

System.out.println(moveResult);

if(moveResult)

现在只调用一次。

关于java - while 循环似乎在完成循环之前重复步骤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21789333/

相关文章:

c - 尽管表达式变为假,但 while 循环不退出

java - 如何在不降低质量的情况下从图像 (JPG) 中删除元数据?

java - 什么时候应该实现 Runnable?

java - Ehcache - 使用 RMI 的复制缓存

asp.net - 枚举中的 VB 括号?

python - 内联 for 循环

java - do-while 循环什么时候比 while 循环更好?

java - 在 java 中使用正则表达式过滤日志

java - 声明后是否可以初始化最终变量..?

c++ - 比较两个密码以在 C++ 中进行验证