我對一個相當簡單的問題表示歉意,但我想問一下 % 運算子。如果我們從 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;
}
uj5u.com熱心網友回復:
這回答了你的問題了嗎?
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://www.uj5u.com/ruanti/378266.html
標籤:爪哇
