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

2012/9/8

"if" Statement?!!!

ManiHallam ManiHallam

2012/9/8

#
Is it possible to have two AND statement like here together, "value = true". If so, how should it be written?
if ((Greenfoot.mouseClicked(Key7.class)) && (Greenfoot.mouseClicked(Key8.class))) && (Counter.getCredit() >= (675))) 
{
}
danpost danpost

2012/9/8

#
Yes. The statement above would not make sense, however, but concatenating ANDs and ORs and using ! (the NOT symbol) can all be used in conjunction within the 'if' phrase. The statement
if (gettingString && aString != null && !"".equals(aString))
where 'gettingString' is a boolean instance variable could be statement in some code.. Each (main, taking into account parenthesis) condition is checked in succession (from left to right) until a value (true or false) can be determined without going to the condition. In other words, in the case above: if 'gettingString' was 'false', processing of the 'if' will immediately return 'false', because it does not matter what results come from the rest of the conditions. If it were 'true', then the second check would be checked. The check to see if 'aString' was NOT an empty string will only be checked if both 'gettingString' were 'true' AND 'aString' was NOT null. Getting back to the 'if' statement you provided in the start of the discussion: Two 'mouseClicked' checks on different objects, ANDed together would never occur (how can you click on two objects at the same time). Unforntunately, even ORing them may not work (this has something to do with how the 'mouseClicked' method is written; you should only use it once per act cycle or on successive calls it will always return 'false').
ManiHallam ManiHallam

2012/9/8

#
I actually want to purchase a chocolate, so I need to enter the item number, which is two digits. For instance, Hobby is number 78, and the price is £6.75. I didn't use
double
code, because I think keeping numbers as
int
is much better than decimal, which doesn't make mess. However, the other thing which makes me confused sometimes, is String. Could you explain to me what is it and how to use it? What I know, it's not a clear picture of it. Many thanks.
danpost danpost

2012/9/8

#
That is where you need to check out the Java tutorials I mentioned in the other discussion. I could explain it, but the documentation is already written (and probably double-checked), plus there are diagrams to help explain there.
ManiHallam ManiHallam

2012/9/8

#
I just send the picture for the vender machine, which you can there see the keypad. all the buttons are as a class like Class NO 7. https://www.dropbox.com/s/guiopfwqtmjiyz3/Screen%20Shot%202012-09-05%20at%2016.06.58.png
ManiHallam ManiHallam

2012/9/8

#
I actually want to keep it simple at this point, but I will update it and put more features in the future for new versions.
ManiHallam ManiHallam

2012/9/8

#
danpost wrote...
That is where you need to check out the Java tutorials I mentioned in the other discussion. I could explain it, but the documentation is already written (and probably double-checked), plus there are diagrams to help explain there.
Thanks ever so much for guidance. :-)
ManiHallam ManiHallam

2012/9/8

#
Dear Danpost, for purchasing a chocolate, user has to click on the numbers on the keypad, which every chocolate has got a two digits code. Therefore, user has to click on to number. As you said if statement doesn't work like the way I did. What about using the clickOn() method inside every single Key Class, then I invoke the methods from Keypad's Subclass in Screen Class, by interaction method. what do you think?
danpost danpost

2012/9/8

#
I think that since each click can be picked up on individually, that you can still have the method in the Keypad class. You will need a 'static String' variable to collect the characters. A static variable is a 'class' variable (not an object or instance one). All the classes will have access to that same individual variable. The Keypad method would look something like this:
import greenfoot.*;

public class Keypad extends Body
{
    static String selection = "";
    char value; // the character that the key represents
    // you may have it as an int and/or a different name

    // your constructors

    public void act()
    {
        if (selection.length() < 2 && Greenfoot.mouseClicked(this)) selection += value;
    }

    // your sufficientFunds and dispense methods
}
In your ChocolateBar class (if that is what you named it, in its act method, you can say
if (myCode.equals(Keypad.selection) && Counter.getValue() >= myPrice)
{
    dispenseMe();
    Keypad.selection = "";
}
Correct me if I am wrong, but did you say that the Counter object is created but not placed into the world? I think I recall you saying that it did not have an image. Please elaborate.
You need to login to post a reply.