如何在 android studio 中将 3 int 从分数加到总分数

How to add 3 int from score to a total score in android studio

我正在尝试从不同级别的游戏中构建记分卡。在android工作室

我试着把 3 个整数加在一起,我也试着把它加倍。但我无法得到总数。我确实得到了每个级别的分数

public class ScoresActivity extends AppCompatActivity {

    TextView tv_total_Score, tvPersonalBest_Begin, tvPersonalBest_Normal, tvPersonalBest_Advance;
    Button btnReturn;

    int totalscore;

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

        SharedPreferences pref = getSharedPreferences("MyPref", 0);
        int scoreSPBG = pref.getInt("scoreSPBG", 0);
        SharedPreferences prefs = getSharedPreferences("MyPref", 0);
        int scoreSP = prefs.getInt("scoreSP", 0);
        SharedPreferences prefz = getSharedPreferences("MyPref", 0);
        int scoreSPAD = prefz.getInt("scoreSPAD", 0);

        SharedPreferences.Editor editor = pref.edit();
        SharedPreferences.Editor editors = prefs.edit();
        SharedPreferences.Editor editorz = prefs.edit();
        editorz.putInt("scoreSAD", scoreSPAD);
        editor.putInt("scoreSPBG", scoreSPBG);
        editors.putInt("scoreSp", scoreSP);
        editors.apply();
        editorz.apply();
        editor.apply();



        tv_total_Score = findViewById(R.id.tv_total_Score);
        tvPersonalBest_Begin = findViewById(R.id.tvPersonalBest_Begin);
        tvPersonalBest_Normal = findViewById(R.id.tvPersonalBest_Normal);
        tvPersonalBest_Advance = findViewById(R.id.tvPersonalBest_Advance);

        btnReturn =findViewById(R.id.btn_return);

        Double n1 = Double.valueOf(tvPersonalBest_Begin.getText().toString());
        double n2 =Double.valueOf(tvPersonalBest_Normal.getText().toString());
       double n3 =Double.valueOf(tvPersonalBest_Advance.getText().toString());        

       double result = n1+n2+n3;
       tv_total_Score.setText("" + result);

         totalscore = scoreSPBG + scoreSP + scoreSPAD;
         tv_total_Score.setText("" + totalscore);


        tvPersonalBest_Begin.setText("" + scoreSPBG);
        tvPersonalBest_Normal.setText("" + scoreSP);
        tvPersonalBest_Advance.setText("" + scoreSPAD);

        btnReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent returnIntent = new  Intent(ScoresActivity.this,MainActivity.class);
                startActivity(returnIntent);
            }
        });

    }

}

我尝试使用 double 并尝试使用 int 进行计算。但是我无法获得我的3个级别的总量。

首先,您有一个错字:

editorz.putInt("scoreSAD", scoreSPAD);

应该是:

editorz.putInt("scoreSPAD", scoreSPAD);

为避免将来出现此类错误,请考虑对首选项键使用常量。

其次,您正在访问首选项以获取存储的值并在不更改它们的情况下立即再次存储它们。请注意,您首先显示结果的变量值,但在下一行中您使用首选项值的总和更新它。因此你总是有零

第三,您将它们作为 int 存储在首选项中,但从视图中将它们作为 double 获取,如果您希望它们有小数,请使用 getDouble 和 putDouble 方法。

第四,虽然我猜你递归调用 ScoresActivity 只是为了调试,但考虑使用 extras 将参数传递给 activity 而不是使用首选项(如果你不打算持久化它们)

首先,你需要做好关于SharedPreference的教程。官方的做得好:enter link description here 在您的情况下,pref、prefs 和 prefz 完全相同,因此您不需要创建 3 个编辑器。另外,为什么要检索偏好分数以在之后注入它们。 所以可能是你没有把score放在sharedpreference中,取回总是0。这就是为什么tv_total_Score显示0。

另外,如果你只有int,就不需要使用Double。 并使用驼峰式命名你的变量,避免下划线。