java <=0?0:1 是什么意思;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21706345/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:10:49  来源:igfitidea点击:

what does mean in java <=0?0:1;

java

提问by Educ

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)

它来自以下代码,它减少了矩阵的元素(禁忌)

 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;
         } 
        }
    }

采纳答案by Shivan Dragon

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;
}

回答by Oded

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#). Ternary means it has three parameters.

这是条件运算符?:,它是 JavaScript 或 Java(以及其他语言,例如 C#)中唯一的三元运算符。三元意味着它具有三个参数。

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. If false, 1 (param3) is returned.

如果为真,则返回 0 (param2)。如果为 false,则返回 1 (param3)。

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]--;

回答by Thilina

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,则答案为 0 否则为 1