Вопрос

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?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top