This site requires JavaScript, please enable it in your browser!
Greenfoot back
CarmanRose
CarmanRose wrote ...

2018/3/21

How do I set a string only once

CarmanRose CarmanRose

2018/3/21

#
I'm creating a project and I'm trying to have a system where it asks for a name if you earn the high score and then displays it, which I have working, but I don't know how to set it so it always stays. The string gets set to the new one and is displayed differently after earning the high score, but after hitting reset it gets set back to nothing (or "") afterwards. How do I make it so once it changes it stays changed?
Yehuda Yehuda

2018/3/21

#
How long are you planning on having it saved for? If you make the string field "static" then instead of getting re-initialized every execution of the program it will reset each compilation, and change whenever(/if) it's changed in the code. If you want to retain its value no matter what happens in the program, then you can save information to a file and set the String to the contents of the file in the beginning of the program, but that wouldn't work online. Online you can use UserInfo which is a whole other (long) topic.
CarmanRose CarmanRose

2018/3/21

#
Just until it's changed in the code or the program is closed, something like that. I don't understand exactly what to do, but I have String input = ""; up in the top, and getWorld().showText(input, 500, 50); if(score>highscore) { input = Greenfoot.ask(prompt); highscore = score; } down later in the code which is where I want it to change. But then how do I make the field static so it stays changed?
Yehuda Yehuda

2018/3/21

#
private static String input = "";
CarmanRose CarmanRose

2018/3/21

#
Thanks, it works!
You need to login to post a reply.