Pregunta

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?

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top