简体   繁体   中英

Why isnt my integer coming up as 0 instead of the value i set in a count down timer?

Below in the code, I have an integer called ship1 and its value is set to 5 in a count down timer object. I want to put this int value into a string array but when I do and when I print the first line of the string array into a text view, ship1AddSpend (the integer) comes up as zero. Why isnt the value changing to 5? can someone help?

final TimerClass Timer2 = (TimerClass) new TimerClass(500,1000) {

    final public int OnFinish()
    {
        ship1AddSpend = 5;
        this.start();
        return ship1AddSpend;
    }

}.start();

int ship1AddSpend;

        final String[] shipDesc = {
                "10 Planets Every 5 Secs \n" + ship1AddSpend + "$",
                "50 Planets Every 5 Secs \n",
                "100 Planets Every 5 Secs \n 1500$",
                "500 Planets Every 4 Secs \n 3000$",
                "1000 Planets Every 4 Secs \n 7500$",
                "5000 Planets Every 4 Secs \n 15000$",
                "10000 Planets Every 3 Secs \n 50000$",
                "30000 Planets Every 3 Secs \n 100000$",
                "60000 Planets Every 3 Secs \n  500000$",
                "100000 Planets Every 1 Secs \n 1000000$"};

What I understand from your code is that you are initializing the string array; particularly the string at the 0th position with the original value of ship1AddSpend which is 0. Once a string is initialized it's value will never change since it is immutable.

What you might want to do instead is prepare the string once the value of your desired variable changes or prepare the string when you want to use/display it. Use a StringBuffer to dynamically prepare the string. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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