编码蝙蝠雪茄三元操作员解决方案

发布于 2025-02-13 02:05:40 字数 315 浏览 1 评论 0原文

public boolean cigarParty(int cigars, boolean isWeekend) {
  if (isWeekend == false) {
    return cigars >= 40 && cigars <= 60 ? true : false;
  }
  if (cigars >= 40) {
    return isWeekend ? true : false;
  }
  else {
    return false;
  }
}

有人对我如何对三元运营商提高效率有任何反馈吗?

public boolean cigarParty(int cigars, boolean isWeekend) {
  if (isWeekend == false) {
    return cigars >= 40 && cigars <= 60 ? true : false;
  }
  if (cigars >= 40) {
    return isWeekend ? true : false;
  }
  else {
    return false;
  }
}

Does anyone have any feedback on how I could be more efficient with ternary operators?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尐籹人 2025-02-20 02:05:40

正如@codebod所说,您的大部分代码都是多余的。

这是使用三元运营商解决它的一种方法:

public boolean cigarParty(int cigars, boolean isWeekend) {
    return (isWeekend) ? (cigars >= 40): (cigars >= 40 && cigars <= 60);
}

这是在没有三元运营商的情况下解决它的一种方法:

public boolean cigarParty(int cigars, boolean isWeekend) {
    if (isWeekend){
        return (cigars >= 40);
    }else{
        return (cigars >= 40 && cigars <= 60);
    }
}

As @codebod said, much of your code is redundant.

This is one way to solve it with ternary operators:

public boolean cigarParty(int cigars, boolean isWeekend) {
    return (isWeekend) ? (cigars >= 40): (cigars >= 40 && cigars <= 60);
}

This is one way to solve it without ternary operators:

public boolean cigarParty(int cigars, boolean isWeekend) {
    if (isWeekend){
        return (cigars >= 40);
    }else{
        return (cigars >= 40 && cigars <= 60);
    }
}
不…忘初心 2025-02-20 02:05:40

这些三元运营商可以完全删除。您的每个返回语句已经评估了所需的布尔结果。与三元运营商一起编写的语句本质上说:如果true返回true,则返回false,这是多余的。

我认为最可读的表格包括三元运营商,如果是这样,则是:

public boolean cigarParty(int cigars, boolean isWeekend) {
    if (cigars < 40){
        return false;
    }
    return isWeekend ? true : cigars <= 60;
}

这也消除了与40的重复比较。

These ternary operators can be removed entirely. Each of your return statements already evaluates to the required boolean result. Your statements as written with the ternary operators essentially say: if true return true, else return false, which is redundant.

I think the most readable form that includes a ternary operator, if that's what's needed, is:

public boolean cigarParty(int cigars, boolean isWeekend) {
    if (cigars < 40){
        return false;
    }
    return isWeekend ? true : cigars <= 60;
}

This also eliminates the repeated comparison of cigars with 40.

神妖 2025-02-20 02:05:40

您实际上根本不需要三元操作员。但是你可以这样做。

  • 如果雪茄&lt; 40,它始终是false
  • 否则,返回true如果后续条件中的任何一个都是正确的。
public static boolean cigarParty(int cigars, boolean isWeekend) {
    return cigars < 40 ? false : (cigars <= 60 || isWeekend);
}

我希望以下内容:

  • 如果雪茄&lt; 40它将返回false和短路表达式。
  • 如果其他任何一个都是true它将返回true
public static boolean cigarParty(int cigars, boolean isWeekend) {
    return cigars >= 40 && (cigars <= 60 || isWeekend);
}

You don't really need ternary operators at all. But you can do it like so.

  • if the cigars < 40, it is always false.
  • otherwise, return true if either of the subsequent conditions are true.
public static boolean cigarParty(int cigars, boolean isWeekend) {
    return cigars < 40 ? false : (cigars <= 60 || isWeekend);
}

I would prefer the following:

  • if cigars < 40 it will return false and short circuit the expression.
  • if either of the others is true it will return true
public static boolean cigarParty(int cigars, boolean isWeekend) {
    return cigars >= 40 && (cigars <= 60 || isWeekend);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文