gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:12:10 26 4
gpt4 key购买 nike

我仍处于编写此程序的早期阶段(突破游戏),但是当尝试测试运行我已经获得的代码时,似乎在一个循环完成之前,在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com