繁体   English   中英

java-使用三元运算符

[英]java - use of ternary operator

我得到了具有4种不同条件的相当大的代码,我尝试使用这里描述的条件三元运算符来缩短代码。 但是,由于我有两个以上的条件,因此我无法管理正确的语法。 有人可以解释在这种情况下如何使用三元运算符吗? 我的代码如下

不,我不是要为我编写代码,而是要寻找有关在多个条件下使用三元运算符的说明

     if (mp.getCurrentPosition() / 1000 / 60 < 10
            && mp.getCurrentPosition() / 1000 % 60 < 10) {
        tvTimeElapsed.setText("0"
                + Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                + ":" + "0"
                + Integer.toString(mp.getCurrentPosition() / 1000 % 60));

    } else if (mp.getCurrentPosition() / 1000 / 60 < 10
            && mp.getCurrentPosition() / 1000 % 60 >= 10) {

        tvTimeElapsed.setText("0"
                + Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                + ":"
                + Integer.toString(mp.getCurrentPosition() / 1000 % 60));

    } else if (mp.getCurrentPosition() / 1000 / 60 >= 10
            && mp.getCurrentPosition() / 1000 % 60 < 10) {

        tvTimeElapsed
                .setText(Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                        + ":"
                        + "0"
                        + Integer.toString(mp.getCurrentPosition() / 1000 % 60));

    } else {

        tvTimeElapsed
                .setText(Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                        + ":"
                        + Integer.toString(mp.getCurrentPosition() / 1000 % 60));

    }

完全没有任何三元的情况如何:

int seconds = mp.getCurrentPosition() / 1000;
tvTimeElapsed.setText(
    String.format("%02d:%02d", seconds / 60, seconds % 60);
);

您无需在这里使用所有这些条件来重新发明轮子:对于所有这些困难的选择和东西,都有一个内部Java字符串格式化程序。

我必须同意所有评论:这很丑。

String textToSet = (mp.getCurrentPosition() / 1000 / 60 < 10 ? 
( mp.getCurrentPosition() / 1000 % 60 < 10 ? "0"
                + Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                + ":" + "0"
                + Integer.toString(mp.getCurrentPosition() / 1000 % 60) : "0"
                + Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                + ":"
                + Integer.toString(mp.getCurrentPosition() / 1000 % 60)) : 
( mp.getCurrentPosition() / 1000 % 60 < 10 ? "0"
                + Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                + ":"
                + Integer.toString(mp.getCurrentPosition() / 1000 % 60) : Integer.toString(mp.getCurrentPosition() / 1000 / 60)
                        + ":"
                        + "0"
                        + Integer.toString(mp.getCurrentPosition() / 1000 % 60)) )

替换整数:

Integer int1 = mp.getCurrentPosition() / 1000 / 60;
Integer int2 = mp.getCurrentPosition() / 1000 % 60;

tvTimeElapsedText = (int1< 10 ? 
                        (int2 < 10 ? 
                            "0" + Integer.toString(int1) + ":" + "0" + Integer.toString(int2) :
                            "0" + Integer.toString(int1) + ":" + Integer.toString(int2)
                        ) :
                        (int2 < 10 ? 
                            Integer.toString(int1) + ":" + "0" + Integer.toString(int2) :
                            Integer.toString(int1) + ":" + Integer.toString(int2)
                        )
                    )

删除了第一部分

编辑:

int var1 = mp.getCurrentPosition() / 1000 / 60;
int var2 = mp.getCurrentPosition() / 1000 % 60;

String hour = var1 < 10 ? "0" + var1 : var1;
String minute = var1 < 10 ? "0" + var2 : var2;

String complete = hour + ":" + minute;

暂无
暂无

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

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