java 带有 || 的 Android Studio If 语句 和 && 运算符

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

Android Studio If statement with || and && operator

javaandroidif-statementis-empty

提问by Sandra123

I'm creating an application which updates users on the score of a football match either in real time or as a final result. At least one score must be inputted in order for the TextViewto be updated and the relevant score to be displayed. I'm checking that at least 1 of a pair of EditTextfields is not empty using the following code:

我正在创建一个应用程序,它可以实时或作为最终结果更新用户的足球比赛比分。必须至少输入一个分数才能TextView更新并显示相关分数。我正在EditText使用以下代码检查一对字段中至少有一个不为空:

if(!(et_current.getText().toString().isEmpty())||(!(et_final.getText().toString().isEmpty()))
&& (!(et_current2.getText().toString().isEmpty())||(!(et_final2.getText().toString().isEmpty()))){
     if(!(et_final.getText().toString().isEmpty()))
              tv_final.setText(et_final.getText().toString());
     else
              tv_current.setText(et_current.getText().toString());

     if(!(et_final2.getText().toString().isEmpty()))
              tv_final2.setText(et_final2.getText().toString());
     else
              tv_current2.setText(et_current2.getText().toString());
}

I want to be able to set the correct TextViewso I have another if statement inside the original if statement to see ensure the correct score is being updated.

我希望能够设置正确的,TextView所以我在原始 if 语句中有另一个 if 语句,以确保更新正确的分数。

When I run the code, I do not seem to be getting past the first if statement. Am I using the correct format or is there an better way to complete these checks?

当我运行代码时,我似乎没有通过第一个 if 语句。我使用的是正确的格式还是有更好的方法来完成这些检查?

Thanks!

谢谢!

回答by RobCo

For readabilities sake, get some variables going

为了可读性,获取一些变量

    boolean currentEmpty = et_current.getText().toString().isEmpty();
    boolean current2Empty = et_current2.getText().toString().isEmpty();
    boolean finalEmpty = et_final.getText().toString().isEmpty();
    boolean final2Empty = et_final2.getText().toString().isEmpty();

And then your code can be much cleaner. Something like

然后你的代码可以更干净。就像是

    if( (!currentEmpty || !finalEmpty) || (!current2Empty || !final2Empty)) {

        if(finalEmpty) {
            tv_current.setText(et_current.getText());
        }
        else {
            tv_final.setText(et_final.getText());
        }

        if(final2Empty) {
            tv_current2.setText(et_current2.getText());
        }
        else {
            tv_final2.setText(et_final2.getText());
        }
    }

I'm not sure if that is completely correct as the requirement is not entirely clear to me, but it should atleast be a good start to follow what's going on.

我不确定这是否完全正确,因为我对这个要求并不完全清楚,但它至少应该是一个很好的开始来了解正在发生的事情。