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

2011/11/17

Making Variable names

kiarocks kiarocks

2011/11/17

#
If i want to create a list of variables(boolean), is it possible to say public Button(int num) { boolean ("b" + num) = true; } Thanks in advance
delmar delmar

2011/11/17

#
The easiest, I think, for something like this is an array of booleans. You declare it like this (if you want, say, six of them): private boolean myBools = new boolean; and then you can write your method like this public void button(int num) { // should check here whether num is between 0 and 5. myBools = true; }
kiarocks kiarocks

2011/11/17

#
Darn, I was hoping that a new variable could be declared dependingon what number object it was.
mjrb4 mjrb4

2011/11/17

#
What scenario are you talking about? There may well be a comparatively simple explanation if we know the context!
-Tigerkralle- -Tigerkralle-

2011/11/17

#
why you don't use arrays? That would be the simplest way!
kiarocks kiarocks

2011/11/17

#
@mjrb4, I don't know what you mean by "which scenario". I do not want an array as i have no idea how many buttons i Will Have. I also do not want to need to remember which button it is, Like in an array.
actinium actinium

2011/11/18

#
'
mjrb4 mjrb4

2011/11/18

#
I wondered if there was a specific scenario on the gallery you were thinking of, that was all. You may be after a map instead of an array? A map is dynamic in size (so you don't need to know how many you'll have) and you could map strings to booleans like so:
Map<String, Boolean> map = new HashMap<>();
map.put("this", true);
map.put("that", false);
map.put("other", true);
The get() method on map would then return the corresponding value, for instance map.get("other"); would return true.
danpost danpost

2011/11/18

#
@mjrb4, What class is 'map' in? It is not imported with greenfoot.*, is it? Actually, just let me know where the documentation on map is. NVM, I found it -- part of Java Collections
kiarocks kiarocks

2011/11/18

#
Ok, that looks like something I'll try
mik mik

2011/11/18

#
The idea with the map is good, but you can do that even a little easier: You can use a set: import java.util.HashSet; ... HashSet<String> buttons = new HashSet<String>(); ... buttons.add("button1"); buttons.add("button7"); So, you take a set and put your things in it. If you just need to store a boolean value, you can interpret the fact that something is in the set as being TRUE, and the fact that it isn't as FALSE. So instead of writing s.th. like if (button2 == true) ... you write if (buttons.contains("button2")) ...
-Tigerkralle- -Tigerkralle-

2011/11/18

#
But why do you write: if(button2 == true) Isn't it enough to write: if(button2) ??? because I always just write 'if(button2)' and it works perfect.
mjrb4 mjrb4

2011/11/18

#
if(button2) is fine, it's equivalent to button2==true. It's just that some people find it easier to understand when it's written mik's way, even though it's more verbose.
kiarocks kiarocks

2011/11/18

#
actually, I just realized how stupid i am. I should Just have an int variable that is set by num. I might use these other ideas in another project, though. Thanks for the replies!
actinium actinium

2011/11/18

#
Just posted a connect 4 in progress scenarios that uses buttons the way you want, i put the code up , the squares on the board are effectively buttons. The buttons light up when the mouse moves over a particular legal square and when you click on a particular square a coin is placed in the square.. Note also when you enter a coin the square above that coin becomes active.
You need to login to post a reply.