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