我的Java游戏中有一个排行榜类,但它并不能完全按照我的要求工作。当您单击排行榜按钮时,第一,第二和第三,我将显示3个得分。我遇到的问题是,每当我得到应替换第二得分的分数时,它都不会改变第三得分。因此,例如:
1st = 10
2nd = 8
3rd = 5
如果我在游戏中得分为9,则看起来是这样:
1st = 10
2nd = 9
3rd = 5
我希望将第3个乐谱替换为原来的第2个乐谱,因此它看起来像:
1st = 10
2nd = 9
3rd = 8
在实现此目标方面的任何帮助将不胜感激!
我的代码:
TextView tvScore, tvPersonalBest, tvSecondBest, tvThirdBest;
int score = getIntent().getExtras().getInt("score");
SharedPreferences pref = getSharedPreferences("MyPref",0);
int scoreSP = pref.getInt("scoreSP",0);
SharedPreferences.Editor editor = pref.edit();
int scoreSB = pref.getInt("scoreSB",0);
SharedPreferences.Editor editorr = pref.edit();
int scoreTB = pref.getInt("scoreTB",0);
SharedPreferences.Editor editorrr = pref.edit();
if(score > scoreSP){
scoreSP = score;
editor.putInt("scoreSP", scoreSP);
editor.commit();
}
if(score > scoreTB && score < scoreSP){
scoreSB = score;
editorr.putInt("scoreSB", scoreSB);
editorr.commit();
}
if(score < scoreSB && score > scoreTB){
scoreTB = score;
editorrr.putInt("scoreTB", scoreTB);
editorrr.commit();
}
tvScore = findViewById(R.id.tvScore);
tvPersonalBest = findViewById(R.id.tvPersonalBest);
tvSecondBest = findViewById(R.id.secondBest);
tvThirdBest = findViewById(R.id.thirdBest);
tvScore.setText(""+score);
tvPersonalBest.setText(""+scoreSP);
tvSecondBest.setText(""+scoreSB);
tvThirdBest.setText(""+scoreTB);
}