Android - 如何简化十的倍数?
Android - How to simplify multiple of ten?
我想要这样的多个值:
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。
谢谢
使用 Java %
operator:
if (currentScore % 10 == 0) {
editor.putInt("TOP_LEVEL", topLevel + 1);
editor.apply();
}
%
运算符returns余数。如果 "currentScore" 除以 10 的余数为 0,则表示 currentScore 是 10 的整数倍。
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;
}
我想要这样的多个值:
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。 谢谢
使用 Java %
operator:
if (currentScore % 10 == 0) {
editor.putInt("TOP_LEVEL", topLevel + 1);
editor.apply();
}
%
运算符returns余数。如果 "currentScore" 除以 10 的余数为 0,则表示 currentScore 是 10 的整数倍。
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;
}