Как сохранить лучшие первые три балла или только лучший результат, я новичок в программировании. У меня есть оценка, но как определить новый счет и старый счет.как сохранить первые три лучшие оценки
public class HighScores extends Activity {
private TextView thighscore1;
private TextView thighscore2;
private TextView thighscore3;
public TextView name;
public int highscore1 =0;
public int highscore2 =0;
public int highscore3 =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_high);
name = (TextView) findViewById(R.id.Names);
thighscore1 = (TextView) findViewById(R.id.highscore1);
thighscore2 = (TextView) findViewById(R.id.highscore2);
thighscore3 = (TextView) findViewById(R.id.highscore3);
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey",
Context.MODE_PRIVATE);
int score = prefs.getInt("key", 0); //0 is the default value
thighscore1.setText("" + score);
if(score > highscore2) {
highscore1 = score;
thighscore1.setText("" + highscore1);
}else{
highscore1=highscore1;
}
if(score > highscore3 && score < highscore1){
highscore2 = score;
thighscore2.setText("" + highscore2);
}else{
highscore2 = highscore2;
}
if (score > 0 && score < highscore2){
highscore3 = score;
thighscore3.setText("" + highscore3);
}else{
highscore3 = highscore3;
}
SharedPreferences sp = this.getSharedPreferences("MyKey",0);
String data = sp.getString("tag", "");
name.setText(""+ data);
}}
спасибо, но я не могу решить мою проблему, я экономить только новые Гол. Как определить более высокий балл private void HighScore() { SharedPreferences prefs = this.getSharedPreferences ("myPrefsKey", Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putInt («ключ», номер); editor.commit(); –
Я не могу точно понять, что вы хотите, но если вы хотите обновить предыдущий рекордер, я просто отредактировал свой ответ. – maRShmallow
Я знаю только: int score1 = prefs.getInt ("Score1", 0); // 0 является значением по умолчанию, как я получаю score2 и score3 SharedPreferences.Editor editor = prefs.edit(); editor.putInt («ключ», счет 1); editor.commit(); –