【问题标题】:How to return boolean "if-then-else" statement in a single return如何在单个返回中返回布尔“if-then-else”语句
【发布时间】:2020-09-19 10:09:40
【问题描述】:

我遇到了 用单个 return 语句替换这个 if-then-else 语句的声纳问题。 由于 if 条件中有三个条件,我如何在没有 if-else 的情况下返回它?

  public boolean isDateRangeExceedOneYear(CardPluginInterface plugin, Date begin, Date end)
  {
    if (!DateTimeToolkit.getInstance().isEmpty(begin)
        && ((CardValidatorInterface) plugin).isValidateYearDateRangeFor()
        && DateTimeToolkit.getInstance().compare(
        DateTimeToolkit.getInstance().add(new DateTime(begin, new Time((short) 0, (short) 0, (short) 0)), 1, 1).date,
        end) <= 0)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

【问题讨论】:

标签: java sonarqube return boolean sonarlint


【解决方案1】:

以上你可以使用

return !DateTimeToolkit.getInstance().isEmpty(begin)
    && ((CardValidatorInterface) plugin).isValidateYearDateRangeFor()
    && DateTimeToolkit.getInstance().compare(
    DateTimeToolkit.getInstance().add(new DateTime(begin, new Time((short) 0, (short) 0, (short) 0)), 1, 1).date,
    end) <= 0;

【讨论】:

    猜你喜欢
    • 2014-12-19
    • 1970-01-01
    • 2021-08-31
    • 2015-01-07
    • 2015-10-17
    • 2011-04-05
    • 2016-11-08
    • 1970-01-01
    • 2020-08-20
    相关资源
    最近更新 更多