简体   繁体   English

将 if/else 转换为三元运算符 Java

[英]Convert if/else to ternary operator Java

My function setA() looks like this:我的 function setA() 看起来像这样:

public double setA(){
    double a;
    
    aField.getText() == null || aField.getText().trim().isEmpty() ? a = 1 : a = Double.parseDouble(aField.getText());

    //a = aField.getText() == null || aField.getText().trim().isEmpty() ? a = 1 : a = Double.parseDouble(aField.getText());

    //return aField.getText() == null || aField.getText().trim().isEmpty() ? a = 1 : a = Double.parseDouble(aField.getText());

    /*if(aField.getText() == null || aField.getText().trim().isEmpty())
        a = 1;
    else
       a = Double.parseDouble(aField.getText());*/
   
    return a;
}

I want to get rid of if/else and rewrite it with the ternary operator.我想摆脱 if/else 并用三元运算符重写它。 None of these 3 ternary options work and, on a build, they show the same mistake:这 3 个三元选项都不起作用,并且在构建中,它们显示相同的错误:

java: unexpected type required: variable found: value java:需要意外类型:找到变量:值

Meanwhile the commented if/else block works just fine.同时注释的 if/else 块工作得很好。 Passing TextField aField into the function and working through this.将 TextField aField 传递到 function 并完成此操作。 doesn't help + before building I see没有帮助 + 在构建之前我看到了

Variable 'a' might not have been initialized变量“a”可能尚未初始化

What's the mistake?有什么错误?

Try this:尝试这个:

a = (aField.getText() == null || aField.getText().trim().isEmpty())
 ? 1.0 : Double.parseDouble(aField.getText());

Looks pretty fine, except one thing the way how you set a variable.看起来很不错,除了你如何设置变量a方式。

double a = <condition> ? <true> : <false>:

You can additionally use Apache utils to simplify a bit the code.您还可以使用Apache来简化代码。

import org.apache.commons.lang3.StringUtils;

public double setA() {
    return StringUtils.isBlank(aField.getText()) ? 1 : Double.parseDouble(aField.getText());
}

public double setA() {
    String str = aField.getText();
    return str == null || str.trim().isEmpty() ? 1 : Double.parseDouble(str.trim());
}

The problem with your ternary attempts is that they are not corrected in terms of syntax:您的三元尝试的问题是它们在语法方面没有得到纠正:

//a = aField.getText() == null || aField.getText().trim().isEmpty() ? a = 1 : a = Double.parseDouble(aField.getText());

For the Java language specification ( §15.25 ):对于 Java 语言规范( §15.25 ):

15.25. 15.25。 Conditional Operator?条件运算符? :

The conditional operator?条件运算符? : uses the boolean value of one expression to decide which of two other expressions should be evaluated. : 使用一个表达式的 boolean 值来决定应该评估其他两个表达式中的哪一个。

ConditionalExpression: ConditionalOrExpression ConditionalOrExpression? ConditionalExpression: ConditionalOrExpression ConditionalOrExpression? Expression: ConditionalExpression表达式:条件表达式

So in your case is a = (conditional expression) ?所以在你的情况下是 a = (条件表达式) value of 'a' if conditional expression is true : value of 'a' if the conditional expression is false;如果conditional expression为真,则“a”的值如果conditional expression为假,则“a”的值;

a = aField.getText() == null || aField.getText().trim().isEmpty() 
  ? 1 
  : Double.parseDouble(aField.getText());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM