检查用户是否至少输入了一项输入的最佳/最有效方法

thu*_*nja 1 java if-statement

我正在尝试从函数中获取多个参数,并且正在检查至少其中一个参数是否不为空或为空。

现在我正在做这样的事情。

void foo(String a, String b, String c, String d, ... other strings){

//make sure at least one of the inputs are not null.
if(a!=null || b!=null || c!=null || d!=null ... more strings){
  //do something with the string
}

}
Run Code Online (Sandbox Code Playgroud)

所以输入可以是foo(null, null, null, "hey"); 但不能是foo(null, null, null, null);

我的问题是有没有更好的方法来做到这一点,而不是不断添加到 if 语句。我现在一片空白……谢谢

Joh*_*int 5

使用可变参数

   public static boolean atLeastOneEmpty(String firstString, String... strings){
      if(firstString == null || firstString.isEmpty())
         return true;

      for(String str : strings){
         if(str == null || str.isEmpty())
            return true;
      }
      return false;

    }
Run Code Online (Sandbox Code Playgroud)

如果至少有一个字符串为空,则返回 true