首页 > 解决方案 > Android - 如何简化十的倍数?

问题描述

我想要这样的多个值:

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。谢谢

标签: javaandroidmultiplication

解决方案


int j = 10;

for (int i = 1; i <= 20 ; i++) {
     if (currentScore == j) {
         editor.putInt("TOP_LEVEL", topLevel + 1);
         editor.apply();
         break;
     }
     j = j + 10;
}

推荐阅读