简体   繁体   English

Android Studio If语句|| 和&&运算符

[英]Android Studio If statement with || and && operator

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 TextView to be updated and the relevant score to be displayed. 必须至少输入一个分数才能更新TextView并显示相关分数。 I'm checking that at least 1 of a pair of EditText fields is not empty using the following code: 我正在使用以下代码检查一对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 TextView so 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. 当我运行代码时,我似乎并没有超越第一个if语句。 Am I using the correct format or is there an better way to complete these checks? 我使用的是正确格式还是有更好的方法来完成这些检查?

Thanks! 谢谢!

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. 我不确定这是否完全正确,因为要求对我来说还不是很清楚,但是应该至少是一个很好的开始来了解正在发生的事情。

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

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