[英]what does mean in java <=0?0:1;
I'm trying to understand this program in java but i'm new to this language. 我试图用Java理解这个程序,但是我是这种语言的新手。
Can you tell me what 你能告诉我什么吗
<=0?0:1;
means? 手段?
It's from the following code that decrements the elements of a matrix (tabu) 是从以下代码中递减的矩阵元素(tabu)
public void decrementTabu(){
for(int i = 0; i<tabuList.length; i++){
for(int j = 0; j<tabuList.length; j++){
tabuList[i][j]-=tabuList[i][j]<=0?0:1;
}
}
}
You are not looking at the operator correctly. 您没有正确看操作员。
This is the conditional operator ?:
, which is the only ternary operator in JavaScript or Java (and other languages, such as C#). 这是条件运算符?:
,它是JavaScript或Java(以及其他语言,例如C#)中唯一的三元运算符。 Ternary means it has three parameters. 三元表示它具有三个参数。
Essentially this is what it means: 本质上这是什么意思:
(condition)?(true branch):(false branch)
param1 param2 param3
In your code example, the condition (param1) is: 在您的代码示例中,条件(param1)为:
tabuList[i][j]<=0
If true, 0 (param2) is returned. 如果为true,则返回0(参数2)。 If false, 1 (param3) is returned. 如果为false,则返回1(参数3)。
The return value is then decremented from tabuList[i][j]
via the -=
operator. 然后,通过-=
运算符从tabuList[i][j]
减小返回值。
The whole statement: 整个语句:
tabuList[i][j]-=tabuList[i][j]<=0?0:1;
Can be written as: 可以写成:
if (tabuList[i][j] > 0)
tabuList[i][j]--;
tabuList[i][j]-=tabuList[i][j]<=0?0:1;
can be written as: 可以写成:
int tabuListEntry = tabuList[i][j];
tabuListEntry -=tabuListEntry <=0?0:1;
can be written as: 可以写成:
int tabuListEntry = tabuList[i][j];
tabuListEntry = tabuListEntry - (tabuListEntry <=0?0:1);
can be written as: 可以写成:
int tabuListEntry = tabuList[i][j];
int decrementAmount = tabuListEntry <=0?0:1;
tabuListEntry = tabuListEntry - decrementAmount ;
can be written as: 可以写成:
int tabuListEntry = tabuList[i][j];
int decrementAmount = 0;
if(tabuListEntry <= 0) {
decrementAmount = 0;
} else {
decrementAmount = 1;
}
tabuListEntry = tabuListEntry - decrementAmount ;
can be written as: 可以写成:
int tabuListEntry = tabuList[i][j];
int decrementAmount = 0;
if(tabuListEntry > 0) {
decrementAmount = 1;
}
tabuListEntry = tabuListEntry - decrementAmount ;
can be written as: 可以写成:
int tabuListEntry = tabuList[i][j];
if(tabuListEntry > 0) {
tabuListEntry = tabuListEntry - 1;
}
This is work as if condition, will take "c<=0?0:1;" 这就像条件一样工作,将取“ c <= 0?0:1;”。
This means if c is less than or equal to zero then answer is 0 else 1 这意味着如果c小于或等于零,则答案为0,否则为1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.