如何保存最好的前三个分数或只有最佳分数,我是编程新手。 我有分数,但是如何定义新分数和旧分数。
th_ppl
答案 0 :(得分:0)
用于在偏好设置中设置分数:
SharedPreferences.Editor editor = getSharedPreferences("PreferenceName",MODE_PRIVATE).edit();
editor.putInt("Score1", highscore1);
editor.putInt("Score2", highscore2);
editor.putInt("Score3", highscore3);
editor.apply();
从偏好设备中检索分数:
SharedPreferences prefs = getSharedPreferences("PreferenceName",MODE_PRIVATE);
if(prefs.getString("text", null) != null) {
int score1 = prefs.getInt("Score1", 0); // 0 is the default value
int score2 = prefs.getInt("Score2", 0);
int score3 = prefs.getInt("Score3", 0);
}
的更多信息
从首选项中删除任何值
editor.remove("highscore1"); // will delete highscore1
答案 1 :(得分:0)
我解决了我的问题,得分也有一个名字:
if (number > prefs.getInt("key", 0)) {
editor.putInt("key", number);
editor.putString("tag", value);
} else if (number < prefs.getInt("key", 0)) {
if (number > prefs.getInt("key1", 0)) {
editor.putInt("key1", number);
editor.putString("tag1", value1);
}
}
if (number < prefs.getInt("key1", 0)) {
editor.putInt("key2", number);
editor.putString("tag2", value2);
}