[英]Return random boolean and use to guide an “if” statement
I've been researching how to return a random boolean and then use that returned boolean to drive an "if" statement, but I can't figure out how to structure or what syntax to use. 我一直在研究如何返回一个随机布尔值,然后使用返回的布尔值来驱动一个“if”语句,但我无法弄清楚如何构造或使用什么语法。
private Random random;
public void mousePressed(MouseEvent e) {
determineHit();
if (random = true) {
//Perform true//
}
else {
//Perform false//
}
private boolean determineHit() {
return random.nextBoolean();
}
How can I get my determineHit method to return the boolean and then plug that back into the main body of the method? 如何让我的determineHit方法返回布尔值,然后将其插回到方法的主体中?
Thanks 谢谢
private Random random;
public void mousePressed(MouseEvent e) {
boolean hitResult = determineHit();
if (hitResult) {
//Perform true//
}
else {
//Perform false//
}
private boolean determineHit() {
return random.nextBoolean();
}
Since an if ()
requires a boolean result, the == true
is implied, no need to explicitly code it. 由于
if ()
需要布尔结果,因此隐含== true
,无需显式编码。
Just make sure that you have instantiated random
before using it, in a constructor for example. 例如,在构造函数中确保在使用之前已经实例化了
random
。
if (random.nextBoolean()) {
//Perform true//
} else {
//Perform false//
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.