什么是问号“?”和冒号“:”运算符用于? [重复]

时间:2022-11-06 22:24:24

This question already has an answer here:

这个问题在这里已有答案:

Two questions about using a question mark "?" and colon ":" operator within the parentheses of a print function: What do they do? Also, does anyone know the standard term for them or where I can find more information on their use? I've read that they are similar to an 'if' 'else' statement.

关于使用问号的两个问题“?”和打印函数括号内的冒号“:”运算符:它们做了什么?此外,是否有人知道他们的标准术语或我可以在哪里找到有关其使用的更多信息?我读过它们与'if''else'语句类似。

int row = 10;
int column;
while (row >= 1)
{
    column = 1;
    while(column <= 10)
    {
        System.out.print(row % 2 == 1 ? "<" : "\r>");
        ++column;
    }
    --row;
    System.out.println();
}

7 个解决方案

#1


247  

This is the ternary conditional operator, which can be used anywhere, not just the print statement. It's sometimes just called "the ternary operator", but it's not the only ternary operator, just the most common one.

这是三元条件运算符,可以在任何地方使用,而不仅仅是print语句。它有时被称为“三元运算符”,但它不是唯一的三元运算符,只是最常见的运算符。

Here's a good example from Wikipedia demonstrating how it works:

以下是*展示其工作原理的一个很好的例子:

A traditional if-else construct in C, Java and JavaScript is written:

编写了C,Java和JavaScript中的传统if-else结构:

if (a > b) {
    result = x;
} else {
    result = y;
}

This can be rewritten as the following statement:

这可以改写为以下语句:

result = a > b ? x : y;

Basically it takes the form:

基本上它采取以下形式:

boolean statement ? true result : false result;

So if the boolean statement is true, you get the first part, and if it's false you get the second one.

因此,如果布尔语句为真,则获得第一部分,如果为假,则获得第二部分。

Try these if that still doesn't make sense:

尝试这些,如果仍然没有意义:

System.out.println(true ? "true!" : "false.");
System.out.println(false ? "true!" : "false.");

#2


7  

Thats an if/else statement equilavent to

这是一个if / else语句等于

if(row % 2 == 1){
  System.out.print("<");
}else{
  System.out.print("\r>");
}

#3


3  

a=1;
b=2;

x=3;
y=4;

answer = a > b ? x : y;

answer=4 since the condition is false it takes y value.

answer = 4,因为条件为假,它需要y值。

A question mark (?)
. The value to use if the condition is true

问号(?)。条件为真时使用的值

A colon (:)
. The value to use if the condition is false

冒号(:)。条件为false时使用的值

#4


2  

Maybe It can be perfect example for Android, For example:

也许它可以是Android的完美示例,例如:

void setWaitScreen(boolean set) {
    findViewById(R.id.screen_main).setVisibility(
            set ? View.GONE : View.VISIBLE);
    findViewById(R.id.screen_wait).setVisibility(
            set ? View.VISIBLE : View.GONE);
}

#5


1  

it is a ternary operator and in simple english it states "if row%2 is equal to 1 then return < else return /r"

它是一个三元运算符,用简单的英语表示“如果行%2等于1则返回

#6


1  

Also just though I'd post the answer to another related question I had,

同样,虽然我发布了我的另一个相关问题的答案,

a = x ? : y;

Is equivalent to:

相当于:

a = x ? x : y;

If x is false or null then the value of y is taken.

如果x为false或null,则取y的值。

#7


0  

They are called the ternary operator since they are the only one in Java.

它们被称为三元运算符,因为它们是Java中唯一的运算符。

The difference to the if...else construct is, that they return something, and this something can be anything:

与if ... else构造的区别在于,它们返回了一些东西,这个东西可以是任何东西:

  int k = a > b ? 7 : 8; 
  String s = (foobar.isEmpty ()) ? "empty" : foobar.toString (); 

#1


247  

This is the ternary conditional operator, which can be used anywhere, not just the print statement. It's sometimes just called "the ternary operator", but it's not the only ternary operator, just the most common one.

这是三元条件运算符,可以在任何地方使用,而不仅仅是print语句。它有时被称为“三元运算符”,但它不是唯一的三元运算符,只是最常见的运算符。

Here's a good example from Wikipedia demonstrating how it works:

以下是*展示其工作原理的一个很好的例子:

A traditional if-else construct in C, Java and JavaScript is written:

编写了C,Java和JavaScript中的传统if-else结构:

if (a > b) {
    result = x;
} else {
    result = y;
}

This can be rewritten as the following statement:

这可以改写为以下语句:

result = a > b ? x : y;

Basically it takes the form:

基本上它采取以下形式:

boolean statement ? true result : false result;

So if the boolean statement is true, you get the first part, and if it's false you get the second one.

因此,如果布尔语句为真,则获得第一部分,如果为假,则获得第二部分。

Try these if that still doesn't make sense:

尝试这些,如果仍然没有意义:

System.out.println(true ? "true!" : "false.");
System.out.println(false ? "true!" : "false.");

#2


7  

Thats an if/else statement equilavent to

这是一个if / else语句等于

if(row % 2 == 1){
  System.out.print("<");
}else{
  System.out.print("\r>");
}

#3


3  

a=1;
b=2;

x=3;
y=4;

answer = a > b ? x : y;

answer=4 since the condition is false it takes y value.

answer = 4,因为条件为假,它需要y值。

A question mark (?)
. The value to use if the condition is true

问号(?)。条件为真时使用的值

A colon (:)
. The value to use if the condition is false

冒号(:)。条件为false时使用的值

#4


2  

Maybe It can be perfect example for Android, For example:

也许它可以是Android的完美示例,例如:

void setWaitScreen(boolean set) {
    findViewById(R.id.screen_main).setVisibility(
            set ? View.GONE : View.VISIBLE);
    findViewById(R.id.screen_wait).setVisibility(
            set ? View.VISIBLE : View.GONE);
}

#5


1  

it is a ternary operator and in simple english it states "if row%2 is equal to 1 then return < else return /r"

它是一个三元运算符,用简单的英语表示“如果行%2等于1则返回

#6


1  

Also just though I'd post the answer to another related question I had,

同样,虽然我发布了我的另一个相关问题的答案,

a = x ? : y;

Is equivalent to:

相当于:

a = x ? x : y;

If x is false or null then the value of y is taken.

如果x为false或null,则取y的值。

#7


0  

They are called the ternary operator since they are the only one in Java.

它们被称为三元运算符,因为它们是Java中唯一的运算符。

The difference to the if...else construct is, that they return something, and this something can be anything:

与if ... else构造的区别在于,它们返回了一些东西,这个东西可以是任何东西:

  int k = a > b ? 7 : 8; 
  String s = (foobar.isEmpty ()) ? "empty" : foobar.toString ();