我仍然處於編寫這個程序的早期階段(BreakThrough遊戲),但是當試圖運行我已經得到的代碼時,似乎在一個循環完成之前do while循環中重複執行步驟。看來它在完成循環之前運行了obj.userMove(row, col, move);
三次。while循環在完成循環之前似乎是重複步驟?
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
是的,但在第一次之後它只調用打印它的返回值,第三次只是爲了確保用戶實際移動。打印返回值是否會導致整個方法再次運行? if語句也一樣?對於調用類仍然很新穎。 – ToonLink
如果你實際上稱它爲3次(和你一樣),它將運行3次。如果您只調用一次,然後僅在其他地方使用返回的值,則它只會運行一次。每次將'obj.userMove(...)'放入代碼中時,都會調用該方法。它不在'System.out.println'裏面。 –