返回值的三元运算符 - Java/Android

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

Ternary operator to return value- Java/Android

javaandroidfunctionoperatorsternary-operator

提问by sumit

Just switched to Java from php

刚从php切换到Java

I encountered following issue

我遇到了以下问题

I want to rewrite

我想重写

if(usrname.equals(username) && (passwd.equals(password))){
    return true;
}
else{
    return false;
}

as

作为

(usrname.equals(username) && passwd.equals(password) )? return true : return false;

it is not working(syntax error) however,

但是,它不起作用(语法错误),

int a=1;
int b=2;
int minVal = a < b ? a : b;

is working

正在工作

Why ternary operator are not behaving correctly while returning value depending on some condition

为什么三元运算符在根据某些条件返回值时行为不正确

EDIT

编辑

return  (usrname.equals(username) && passwd.equals(password)); 

could be solution if it return boolean .

如果它返回 boolean 可能是解决方案。

lets say i need

可以说我需要

 (usrname.equals(username) && passwd.equals(password) )? return "member": return "guest";

采纳答案by Aniket Thakur

You can do

你可以做

return (usrname.equals(username) && passwd.equals(password) )?  true : false;

trueand falsecan be replaced by any return value you want. If it is just boolean then you can avoid ternary operator altogether. Just do

true并且false可以替换为您想要的任何返回值。如果它只是布尔值,那么您可以完全避免使用三元运算符。做就是了

return  (usrname.equals(username) && passwd.equals(password));

回答by Ruchira Gayan Ranaweera

Why redundant boolean

为什么冗余 boolean

Just use

只需使用

return  (usrname.equals(username) && passwd.equals(password));

回答by Troncador

By the way you can simplify:

顺便说一下,您可以简化:

return (usrname.equals(username) && passwd.equals(password) )? return true : return false;

返回 (usrname.equals(username) && passwd.equals(password) )?返回真:返回假;

To:

到:

return usrname.equals(username) && passwd.equals(password);

返回 usrname.equals(username) && passwd.equals(password);

The ternary operator work similar in php than Java, I think you have made a silly mistake, maybe "username" have a space or another white character

三元运算符在 php 中的工作方式与 Java 相似,我认为您犯了一个愚蠢的错误,也许“用户名”有一个空格或另一个白色字符

String a1 = "a";
String b1 = "b";

String a2 = "a";
String b2 = "b";

System.out.println((a1.equals(a2)&&b1.equals(b2))?"true":"false");

it return "true"

它返回“真”

回答by Stephen C

lets say I need

  (usrname.equals(u) && passwd.equals(p)) ? return "member" : return guest";

可以说我需要

  (usrname.equals(u) && passwd.equals(p)) ? return "member" : return guest";

The correct syntax is:

正确的语法是:

   return (usrname.equals(u) && passwd.equals(p)) ? "member" : "guest";


The general form of the ternary operator is

三元运算符的一般形式是

   expression-1 ? expression-2 : expression-3

where expression-1has type boolean, and expression-2and expression-3have the same type1.

其中expression-1具有类型boolean,并且expression-2expression-3具有相同的类型1

In your code, you were using returnstatements where expressions are required. In Java, a returnstatement is NOT a valid expression.

在您的代码中,您使用了return需要表达式的语句。在 Java 中,return语句不是有效的表达式。

1 - This doesn't take account of the conversionsthat can take. For the full story, refer to the JLS.

1 - 这没有考虑可以进行的转换。有关完整故事,请参阅 JLS。



Having said that, the best way to write yourexample doesn't uses the conditional operator at all:

说了这么多,写的最好方法例子并不用条件运算都:

   return usrname.equals(username) && passwd.equals(password);

回答by Sameer Sinha

Java ternary operator is an expression, and is thus evaluated to a single value. As per the Javadocs, for below given expression

Java 三元运算符是一个表达式,因此被评估为单个值。根据Javadocs,对于下面给定的表达式

result = someCondition ? value1 : value2;

if the boolean condition is true then assign the value of value1to the result, else assign value2to the result. Both value1 and value2 should be of same value type.

如果布尔条件为真,则将value1的值分配给结果,否则将value2分配给结果。value1 和 value2 都应该是相同的值类型。

Hence the correct syntax of a return statement would be

因此 return 语句的正确语法是

return boolean_condition ? value_when_true : value_when_false