java - Android 中的评分(连续添加并保存 HighScore 分数)

标签 java android

下面是我使用的代码。它的工作直到 3 次点击才有效,后续点击不会像游戏中的正常得分那样“连续”添加。另一个问题是我不知道时间到后如何保存分数。我正在考虑使用数据(保存的分数)来显示每场比赛结束时的最佳分数。任何建议都将受到高度赞赏。

MainActivity.java

 //CALCULATE SCORE
    //declare a boolean variable for score
    private int optionTxtView = 0  ;
    private int addClick = 0  ;



    private void calculate(){
        x = Integer.parseInt(tv3.getText().toString().replaceAll("\\s",""));
        y = Integer.parseInt(tv2.getText().toString().replaceAll("\\s",""));
        z = x + y;
        score.setText(Integer.toString(z));
    }

    private void calculate2(){
        x = Integer.parseInt(score2.getText().toString().replaceAll("\\s",""));
        y = Integer.parseInt(tv2.getText().toString().replaceAll("\\s",""));
        z = x + y;
        score.setText(Integer.toString(z));
    }

    private void calculate3(){
        x = Integer.parseInt(score2.getText().toString().replaceAll("\\s",""));
        y = Integer.parseInt(score3.getText().toString().replaceAll("\\s",""));
        z = x + y;
        score.setText(Integer.toString(z));
    }

     //search 
    public void viewWord(View view)
    {   
        String s1= search.getText().toString();
        String s2= dbHelper.getData(s1);

        if(optionTxtView == 0){
        //display the score on textview1
            tv2.setText(s2);
            optionTxtView = 1;

            }
        else{
            if(optionTxtView == 1){
        //display the score on textview2    
            tv3.setText(s2);
            optionTxtView = 0;
            }
        }

        adapter.add(text.getText().toString());
        adapter.notifyDataSetChanged();
        text.clearComposingText();    

      //clicks = calculate to be use
        if(addClick == 0){
            calculate();
            score2.setText(score.getText());
            addClick = 1;
            text.clearComposingText();

        }
        else{
            if(addClick == 1){
                calculate();
                score2.setText(score.getText());
                addClick = 2;
                text.clearComposingText();

            }
            else{
                if(addClick == 2){
                    calculate2();
                    score2.setText(score.getText());
                    addClick = 3;
                    text.clearComposingText();
                }
                else{
                    if(addClick == 3){
                        calculate3();
                        score3.setText(score.getText());
                        addClick = 2;
                        text.clearComposingText();
                    }
            }

        }
    }

}

最佳答案

要保存标记,您可以使用 SharedPrefences:

保存您的分数: (gameData 是稍后用于获取共享首选项的字符串)

SharedPreferences myData= getSharedPreferences("gameData", 0);
SharedPreferences.Editor editor = myData.edit();
editor.putInt("gameScore", myGameScore);
editor.commit;

加载您的分数:

SharedPreferences myData= getSharedPreferences("gameData", 0);
int myScore = myData.getInt(gameScore, 0);

执行此操作后,“myScore”将包含您保存的分数(如果您之前没有保存任何分数,则默认值为 0)。

关于你问题的第一部分 - 我没有理解你问的问题......

关于java - Android 中的评分(连续添加并保存 HighScore 分数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27150181/

相关文章:

java.lang.ClassCastException : com. google.gdata.data.TextContent 无法转换为 com.google.gdata.data.OutOfLineContent

java - 为什么我的自定义 ListView 不显示任何内容?

java - 使用计时器计算秒数并更新 Activity 中的字符串

java - Spring MVC、Maven、Pivo​​tal tc 服务器 HTML 404 未找到错误

java - 如何在 Java 中从 wsdl 文件运行 Web 服务

java - 使用flywaydb时如何对存储过程进行版本控制?

java - 在 Android (java) 上解析 json 时出错

android - 找不到类 'android.widget.ThemedSpinnerAdapter' [Android Studio]

android - 在 Android 上计算测验的分数

java - 选择文本中的所有模式