简体   繁体   English

制作自定义条件语句-Java

[英]Make a Custom Conditional Statement - Java

Is there a way to make a custom conditional statement? 有没有办法制作自定义条件语句? Or is this not possible and I would have to just put the function inside of an if . 还是这是不可能的,我只需要将该函数放在if

For example: 例如:

customConditional(bar){
    //run this code
}

Where if bar was 1 it would execute the code and any other number wouldn't. 如果bar1 ,它将执行代码,而其他任何数字都不执行。

Edit: What I was trying to say was, how are conditionals defined and is there a way to define another one? 编辑:我想说的是,如何定义条件,是否有一种方法可以定义另一个条件?

No need to do that, all you need is a method that will return a true/false statement then use it inside an if condition: 无需这样做,您所需要的就是一个将返回true / false语句,然后在if条件中使用它的方法:

Example: 例:

private boolean myOwnMethod(int bar) {
    return bar == 1;  //this check can be as simple or complex as you need
}

Usage: 用法:

if (myOwnMethod(bar)) {
    //run this code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM