Java 有人可以向我解释这些代码吗?(我 % 2 == 0 ?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21242926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can someone explain thos code to me? (i % 2 == 0 ?)
提问by user3216557
I'm trying to work through a project which creates a chessboard layout. The only part I can't get my head around is this line
我正在尝试完成一个创建棋盘布局的项目。我唯一无法理解的部分是这条线
if (row == 0)
singleSquare.setBackground( i % 2 == 0 ? Color.black : Color.white );
else if (row != 0)
singleSquare.setBackground( i % 2 == 0 ? Color.white : Color.black );
It seems to be 2 nested if
statements but can anyone explain the logic behind it? It basically makes one square white then next square black etc for and 8,8 grid but I can't understand the logic. Thanks
它似乎是 2 个嵌套if
语句,但谁能解释它背后的逻辑?它基本上使一个正方形白色然后下一个正方形黑色等和 8,8 网格,但我无法理解逻辑。谢谢
Edit - Should have been more precise. What does this part mean?
编辑 - 应该更精确。这部分是什么意思?
i % 2 == 0 ? Color.black : Color.white
I will swap between 1 and 0 as the loop goes through I understand. What does the question mark do that makes it change from black to white?
当循环通过时,我将在 1 和 0 之间交换我理解。问号做了什么使它从黑色变为白色?
采纳答案by Jeroen Vannevel
It's a ternary statement combined with the modulus operator.
这是一个与模运算符结合的三元语句。
Ternary structure:
三元结构:
condition ? action-true : action-false
健康)状况 ?动作真:动作假
this results in your case to
这导致你的情况
i % 2 == 0 ? Color.black : Color.white
Or written fully:
或者写完整:
if(i % 2 == 0) {
singleSquare.setBackground(Color.black);
} else {
singleSquare.setBackground(Color.white);
}
Modulus:
模数:
i % 2
returns the remainder after divising i
by 2. In your case this will result in an alternation between black and white (by adding 1 each iteration it will alternatively evaluate to true
and false
).
i % 2
返回除以i
2后的余数。在您的情况下,这将导致黑色和白色之间的交替(通过每次迭代加 1,它将交替评估为true
和false
)。
回答by Josh Engelsma
i %2 == 0
is what is generally used to determine if an index is an even index.
i %2 == 0
通常用于确定索引是否为偶数索引。
In your example this means that the even indexes for row 0 will be set to black.
在您的示例中,这意味着第 0 行的偶数索引将设置为黑色。
The other rows not equal to zero will also have every other spot being black or white. However they will be white for the even indexes.
不等于零的其他行也将每隔一个点是黑色或白色。但是,对于偶数索引,它们将是白色的。
Perhaps you are having trouble interpreting the ternary or ? operator in the statement below.
也许您在解释三元或 ? 运算符在下面的语句中。
( i % 2 == 0 ? Color.black : Color.white )
Basically this is saying, if condition i%2 == 0
(index is even), carry out the first instructions after the question mark (set the color to black), else carry the instructions out after the colon (set the color to white).
基本上这就是说,如果条件i%2 == 0
(索引为偶数),则执行问号后的第一条指令(将颜色设置为黑色),否则执行冒号后的指令(将颜色设置为白色)。
回答by Keppil
if (row == 0)
singleSquare.setBackground( i % 2 == 0 ? Color.black : Color.white );
This means that if row == 0
(you probably have something like row = i % 2;
above, making the rows alternate between 0
and 1
), every other square will be black, and every other one will be white.
这意味着如果row == 0
(您可能有类似row = i % 2;
上面的内容,使行在0
和之间交替1
),每隔一个方块将是黑色的,而每隔一个方块将是白色的。
For row = 1
you have the same case, but starting with white.
因为row = 1
你有同样的情况,但从白色开始。
You can read more about modulo (%
) and Java's other operators here.
您可以在此处阅读有关 modulo ( %
) 和 Java 其他运算符的更多信息。
EDIT
Added the following after OP's clarifications:
编辑
在 OP 澄清后添加了以下内容:
singleSquare.setBackground(i % 2 == 0 ? Color.black : Color.white)
uses a ternary expressionand is the same as
使用三元表达式并且与
if (i % 2 == 0) {
singleSquare.setBackground(Color.black);
} else {
singleSquare.setBackground(Color.white);
}
回答by monkeyhybrid
%
is the modulus operator. It produces the remainder of a division operation. So 3 % 2 == 5
.
%
是模运算符。它产生除法运算的余数。所以3 % 2 == 5
。
The ?
is called a ternary operator. It can be used as a shorthand type of if/else statement. If the part to the left of the ternary operator evaluates to be true, then the value immediately following the ternary operator is used. If the part to the left of the ternary operator evaluates to be false, then the second value (following the :
symbol) is used.
在?
被称为三元运算符。它可以用作 if/else 语句的简写类型。如果三元运算符左侧的部分计算结果为真,则使用紧跟在三元运算符之后的值。如果三元运算符左侧的部分计算结果为假,则使用第二个值(在:
符号之后)。
So in your first example, presuming i
is an index of the chess board's column, the value supplied to setBackground() is Color.black
when i
is an even number (because i % 2 == 0
) and is Color.white
when i
is an odd number (because i % 2 != 0
).
因此,在您的第一个示例中,假设i
是棋盘列的索引,提供给 setBackground() 的值是Color.black
wheni
是偶数(因为i % 2 == 0
),而Color.white
何时i
是奇数(因为i % 2 != 0
)。