Android-如何简化十的倍数?

时间:2018-11-24 02:57:08

标签: android multiplication

我想要多个这样的值:

if (currentScore == 10 | currentScore == 20 | currentScore == 30 | currentScore == 40 | currentScore == 50 | currentScore == 60
            | currentScore == 70 | currentScore == 80 | currentScore == 90 | currentScore == 100 | currentScore == 110
            | currentScore == 120 | currentScore == 130 | currentScore == 140 | currentScore == 150 | currentScore == 160
            | currentScore == 170 | currentScore == 180 | currentScore == 190 | currentScore == 200) {
        editor.putInt("TOP_LEVEL", topLevel + 1);
        editor.apply();
    }

如何简化该代码,以便可以计算许多currentScore。 谢谢

2 个答案:

答案 0 :(得分:1)

使用Java % operator

if (currentScore % 10 == 0) {
    editor.putInt("TOP_LEVEL", topLevel + 1);
    editor.apply();
}

%运算符返回余数。如果“ currentScore”除以10的余数为0,则意味着currentScore是10的精确倍数。

答案 1 :(得分:1)

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference
    at com.example.asus.courest.ViewStudentRecord.onCreate(ViewStudentRecord.java:39)