Java 条件(检查条件中的第一个条件)

发布于 2025-01-08 05:31:22 字数 566 浏览 0 评论 0原文

if((x == 5) || (x == 2)) {  
    [huge block of code that happens]  
    if(x == 5)  
        five();  
    if(x == 2)  
        two();  
}

所以我要检查 5 或 2。在 5 或 2 之后会发生一大堆代码。问题是,然后我想根据它是 5 还是 2 做不同的事情。我没有'不想对巨大的代码块有单独的 5 或 2 条件(复制它会很麻烦)。我也不喜欢上面的做法,因为 x 实际上非常长。

有没有办法这样说:

if((x == 5) || (x == 2)) {  
    [huge block of code that happens]  
    if(first conditional was true)  
        five();  
    if(second conditional was true)  
        two();  
}  

我总是可以像上面那样做。只是好奇是否存在这样的选择。

if((x == 5) || (x == 2)) {  
    [huge block of code that happens]  
    if(x == 5)  
        five();  
    if(x == 2)  
        two();  
}

So I'm checking for either 5 or 2. And there is a huge block of code that happens after either 5 or 2. The problem is that then I want to do different things depending on whether it is 5 or 2. I didn't want to have separate conditionals for 5 or 2 for the huge block of code (duplicating it would be unwieldy). I also didn't like the way I did it above because x is actually really long.

Is there a way to say something like:

if((x == 5) || (x == 2)) {  
    [huge block of code that happens]  
    if(first conditional was true)  
        five();  
    if(second conditional was true)  
        two();  
}  

I can always do it the way I did above. Just curious if such an option exists.

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

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

发布评论

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

评论(4

清风疏影 2025-01-15 05:31:23

我能想到的一种方法基本上是在 if 条件中“别名”较长的布尔表达式:

boolean expr1, expr2;

if (expr1 = (x == 5) | expr2 = (x == 2)) {  
    // huge block of code that happens
    if (expr1) five();
    if (expr2) two();
}

我使用非短路运算符来确保 expr2 被分配。

One way I can think of is basically to "alias" the longer boolean expressions in the if condition:

boolean expr1, expr2;

if (expr1 = (x == 5) | expr2 = (x == 2)) {  
    // huge block of code that happens
    if (expr1) five();
    if (expr2) two();
}

I used non short-circuiting operator to ensure expr2 gets assigned.

唔猫 2025-01-15 05:31:23

也许是这样的:

final boolean firstCondition = (x == 5);
final boolean secondCondition = (x == 2);

if (firstCondition || secondCondition) {
    // code
    if(firstCondition) {
        // code 
    }
    else if (secondCondition) {
        // code
    }
}

Maybe something like this:

final boolean firstCondition = (x == 5);
final boolean secondCondition = (x == 2);

if (firstCondition || secondCondition) {
    // code
    if(firstCondition) {
        // code 
    }
    else if (secondCondition) {
        // code
    }
}
小女人ら 2025-01-15 05:31:23

我唯一能想到的就是为这两个选项设置一个标志。有点像这样:

boolean wasFive = x == 5;
boolean wasTwo = x == 2;

if(wasFive || wasTwo) {  
   [huge block of code that happens]  
   if(wasFive)  
       five();  
   if(wasTwo)  
       two();  
 } 

Only thing I can think of would be to set a flag for both options. Sort of like this:

boolean wasFive = x == 5;
boolean wasTwo = x == 2;

if(wasFive || wasTwo) {  
   [huge block of code that happens]  
   if(wasFive)  
       five();  
   if(wasTwo)  
       two();  
 } 
灯下孤影 2025-01-15 05:31:22

如果条件又大又难看,而且远不如 x == 5 好,那么只需将结果存储在布尔值中即可:

boolean xWasFive = x == 5;
boolean xWasTwo = !xWasFive && x == 2;
if (xWasFive || xWasTwo) {
  ...
  if (xWasFive) doA;
  else if (xWasTwo) doB;
}

If the conditionals are big, ugly, and much less nice than x == 5, then just store the results in a boolean:

boolean xWasFive = x == 5;
boolean xWasTwo = !xWasFive && x == 2;
if (xWasFive || xWasTwo) {
  ...
  if (xWasFive) doA;
  else if (xWasTwo) doB;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文