Android: ¿Cómo salvar la mejor puntuación en un juego?

Lolek milagro:

Soy un programador principiante y creo un juego para Android y me encontré con un problema que quiero después de terminar el juego que me muestre la mejor puntuación que logré alcanzar en este juego que hice una condición

if(DOD> sharedPreferences.getInt("DOD_SHA",0));

por supuesto que utilizo SharedPreferences para salvar la mejor puntuación aquí, pero esta condición no funciona en absoluto y mi sharedPreference toma cualquier valor aún inferior a la última puntuación

public class EndScreen extends AppCompatActivity {

    TextView Levelwys,war;
    Button GotoMenu;
    int DOD,ODE,MNO,PIE,DZI,POT,SIL,LOG;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_end_screen);

        Levelwys = findViewById(R.id.textView15);
        war = findViewById(R.id.textView19);
        GotoMenu = findViewById(R.id.button);
        sharedPreferences = getSharedPreferences("com.example.goodmath", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();

        Intent intent = getIntent();
        int number = intent.getIntExtra(GameSingleActivity.LEVEL, 0);
        DOD = intent.getIntExtra("DOD", 0);
        ODE = intent.getIntExtra("ODE", 0);
        MNO = intent.getIntExtra("MNO", 0);
        PIE = intent.getIntExtra("PIE", 0);
        DZI = intent.getIntExtra("DZI", 0);
        POT = intent.getIntExtra("POT", 0);
        SIL = intent.getIntExtra("SIL", 0);
        LOG = intent.getIntExtra("LOG", 0);
        Levelwys.setText(String.valueOf(number));
        String test = String.valueOf(DOD);
        Toast.makeText(getApplicationContext(), test, Toast.LENGTH_LONG).show();
        Save();

        GotoMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GotoMenu();
            }
        });
    }
    @Override
    public void onBackPressed() {

    }

    void GotoMenu()
    {
        Intent intent1 = new Intent(this, MenuActivity.class);
        startActivity(intent1);
    }

    void Save()
    {
        if(DOD> sharedPreferences.getInt("DOD_SHA",0));
        {
            editor.putInt("DOD_SHA",DOD);
            editor.apply ();
            editor.commit();
            Toast.makeText(getApplicationContext(), "cos", Toast.LENGTH_LONG).show();
            war.setText(String.valueOf(sharedPreferences.getInt("DOD_SHA",DOD)));
        }
    }

}
Andropogon:

Tiene error tipográfico en su condición si:

if(DOD> sharedPreferences.getInt("DOD_SHA",0));

Ha agregado punto y coma adicional al final de esta línea. Debido a esto, el código de abajo esta condición siempre fue llamado. Eliminar este punto y coma y todo debería funciona bien:

if(DOD> sharedPreferences.getInt("DOD_SHA",0))
    {
        editor.putInt("DOD_SHA",DOD);
        editor.apply ();
        editor.commit();
        Toast.makeText(getApplicationContext(), "cos", Toast.LENGTH_LONG).show();
        war.setText(String.valueOf(sharedPreferences.getInt("DOD_SHA",DOD)));
    }

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=296708&siteId=1
Recomendado
Clasificación