Domanda

I am writing an interface for the logic gates. I have the following code:

Then for the and gate I coded:

public class And 
{
    public boolean ope(
    {
        assert();

   }
}

Is there any way that I can change this to avoid assert and remove the ellipsis?

È stato utile?

Soluzione

You could write the method so it accepts an arbitrary number of booleans. If any of the booleans is false, return false; otherwise, return true.

public boolean ope(boolean...list)
{
    for (boolean value: list) {
        if (!value) {
            return false;
        }
    }

    return true;
}

Note that this would return true for an empty list. That may or may not be what you want.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top