我的回答是一个相当简单的问题,但我想问一下%操作符。如果我们从0迭代到任何大的数字,并且有代码
int++;
int %= 2;它到底是做什么的?我们设置//int=int%2了吗?它将如何与实际数字的示例一起工作,以及int %= 2;行返回什么?
private void play() {
// while the board does not have a winner neither is full
while(!board.hasWinner() && !board.isFull()) {
//print the state of the game
update();
//set field for the current player and the passed mark
board.setField(players[current].determineMove(board), players[current].getMark());
//go to the next player
current++;
// current= current % 2
//
current %= 2;
}
update();
printResult();
}电流从何而来:
private int current;
// -- Constructors -----------------------------------------------
/**
* Creates a new Game object.
* @requires s0 and s1 to be non-null
* @param s0 the first player
* @param s1 the second player
*/
public Game(Player s0, Player s1) {
board = new Board();
players = new Player[NUMBER_PLAYERS];
players[0] = s0;
players[1] = s1;
current = 0;
}发布于 2021-12-10 00:13:00
这能回答你的问题吗?
int j = 0;
for (int i = 0; i < 10; i++) {
j++;
j %= 2;
System.out.print(j + " ");
}输出:
1 0 1 0 1 0 1 0 1 0对于更整洁的代码,您可以在一行中实现:
j = ++j % 2;https://stackoverflow.com/questions/70297949
复制相似问题