简体   繁体   English

Android应用程式上的Java Timer

[英]Java Timer on an Android app

I need a little help on my android app in java. 我在Java中的android应用上需要一些帮助。

I have a timer that check if current score is better than the best score saved and save it accordingly 我有一个计时器,用于检查当前分数是否比保存的最佳分数更好,并进行相应保存

Here is the code : 这是代码:

    if(sec< bestSec){
        Main.settings= Main.context.getSharedPreferences(GameConstants.PREFS_NAME, Main.MODE_WORLD_WRITEABLE);
        Main.editor= Main.settings.edit();
        if(mSec<10){
            Main.editor.putString("best", sec+"."+"0"+mSec);
        }else{
            Main.editor.putString("best", sec+"."+mSec);
        }
        Main.editor.putInt("bestSec", sec);
        Main.editor.putInt("bestMSec", mSec);
        Main.editor.commit();
    }else if(sec== bestSec){
        if(mSec< bestMSec){
            Main.settings= Main.context.getSharedPreferences(GameConstants.PREFS_NAME, Main.MODE_WORLD_WRITEABLE);
            Main.editor= Main.settings.edit();
            if(mSec<10){
                Main.editor.putString("best", sec+"."+"0"+mSec);
            }else{
                Main.editor.putString("best", sec+"."+mSec);
            }
            Main.editor.putInt("bestSec", sec);
            Main.editor.putInt("bestMSec", mSec);
        }
    }

My best score is : 12,89sec for exemple. 我的最高成绩是:例如12,89sec。 If I do 11,20 or 11,92 it save nice, but if I do 12,45sec it doesn't save. 如果我执行11,20或11,92,则保存效果很好,但是如果执行12,45sec,则不会保存效果。

Thx for your help ! 谢谢您的帮助!

You should use double or float values to compare them correctly. 您应该使用double或float值来正确比较它们。 If you are casting your decimal values to int, 12,89 and 12,45 will be equal to 12. The decimal part of the number will be ignored. 如果将十进制值转换为int,则12,89和12,45将等于12。数字的小数部分将被忽略。

Use doubles or floats to make effective comparisons and use editor.putFloat() to save the values. 使用double或float进行有效的比较,并使用editor.putFloat()保存值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM