检查随机数的布尔方法
Boolean method to check a random number
我有一个 class 可以在给定的时间间隔内生成一个随机数。
public class Dice{
//code.....
public int throwDice(){
Random randomGen = new Random();
int min = 1;
int max = sides; //sides gets a value earlier in the class
int randomNum = randomGen.nextInt((max - min) + 1) + min;
return randomNum;
}
然后作业说我将使用如下所示的 boolean
方法(其中也包含我已经编写的代码):
private boolean step(){
int res = this.dice.throwDice();
int startpos = this.bridge.getFirst();
if (res == 10){ //this works
return false;
}
else if (res => 7 && res <= 9 ){ //but the error occurs here
//code....
return true;
}
我需要做的是检查throwDice()
方法中生成的随机数是否在一定的数字区间内。
问题是我收到一条错误消息,提示我无法将 int
转换为 boolean
。有办法解决这个问题吗?还是我必须重新考虑我的整个解决方案?
=>
应该是反过来的
else if (res => 7 && res <= 9 ){
应该是
else if (res >= 7 && res <= 9 ){
等式和关系运算符
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to <-----
< Less than
<= Less than or equal to
我有一个 class 可以在给定的时间间隔内生成一个随机数。
public class Dice{
//code.....
public int throwDice(){
Random randomGen = new Random();
int min = 1;
int max = sides; //sides gets a value earlier in the class
int randomNum = randomGen.nextInt((max - min) + 1) + min;
return randomNum;
}
然后作业说我将使用如下所示的 boolean
方法(其中也包含我已经编写的代码):
private boolean step(){
int res = this.dice.throwDice();
int startpos = this.bridge.getFirst();
if (res == 10){ //this works
return false;
}
else if (res => 7 && res <= 9 ){ //but the error occurs here
//code....
return true;
}
我需要做的是检查throwDice()
方法中生成的随机数是否在一定的数字区间内。
问题是我收到一条错误消息,提示我无法将 int
转换为 boolean
。有办法解决这个问题吗?还是我必须重新考虑我的整个解决方案?
=>
应该是反过来的
else if (res => 7 && res <= 9 ){
应该是
else if (res >= 7 && res <= 9 ){
等式和关系运算符
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to <-----
< Less than
<= Less than or equal to