Java 为什么我的经纪人都是女性?
我有10个通用代理:Java 为什么我的经纪人都是女性?,java,repast-simphony,Java,Repast Simphony,我有10个通用代理: public class Agent { private Context<Object> context; private Geography<Object> geography; public int id; public boolean isFemale; public double random; public Agent(Context<Object> context, Geograph
public class Agent {
private Context<Object> context;
private Geography<Object> geography;
public int id;
public boolean isFemale;
public double random;
public Agent(Context<Object> context, Geography<Object> geography, boolean isFemale, double random) {
this.context = context;
this.geography = geography;
this.isFemale = isFemale;
this.random = random;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isFemale() {
return isFemale;
}
public void setFemale(boolean isFemale) {
this.isFemale = isFemale;
}
public double getRandom() {
return random;
}
public void setRandom(double random) {
this.random = random;
}
public void methods {
... does things
}
公共类代理{
私人语境;
私人地理;
公共int id;
公共场所为女性;
公共双随机;
公共代理(上下文、地理、布尔值、双随机){
this.context=上下文;
这个。地理=地理;
this.isFemale=isFemale;
这个。随机=随机;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共布尔值(){
回报是女性;
}
public void setFemale(布尔值isFemale){
this.isFemale=isFemale;
}
公共双getRandom(){
返回随机;
}
公共无效设置随机(双随机){
这个。随机=随机;
}
公开无效方法{
…做事
}
代理是在地理上下文(纬度和经度)中创建的。我正在尝试将代理随机构造为男性或女性。我在上下文生成器中用于创建代理的代码如下:
Agent agent = null;
boolean isFemale = false;
for (int i = 0; i < 10; i++) {
double random = RandomHelper.nextDoubleFromTo(0, 1);
if (random > 0.33){
isFemale = true;
}
agent = new Agent(context, geography, isFemale, random);
context.add(agent);
Coordinate coord = new Coordinate(-79.6976, 43.4763);
Point geom = fac.createPoint(coord);
geography.move(agent, geom);
}
Agent=null;
布尔值isFemale=false;
对于(int i=0;i<10;i++){
double random=RandomHelper.nextDoubleFromTo(0,1);
如果(随机>0.33){
isFemale=正确;
}
代理=新代理(上下文、地理位置、isFemale、随机);
添加(代理);
坐标坐标坐标=新坐标(-79.6976,43.4763);
点几何=工厂创建点(坐标);
地理。移动(代理,geom);
}
当我测试代码时,我发现他们都是女性。我做错了什么?如果我认为他们都是男性,因为布尔值默认为false。因为一旦布尔值设置为true,它就会保持这种状态(您从未将其设置为false,它只是If的一个分支)
顺便说一句,捕捉问题标题您的
布尔值
一旦变为isFemale=true
就不会在每次迭代中更新,其他值仍为true。您可以添加其他部分将其设置为false
for (int i = 0; i < 10; i++) {
isFemale = false;//Set it here
double random = RandomHelper.nextDoubleFromTo(0, 1);
if (random > 0.33){
isFemale = true;
//...
或
那么,
RandomHelper.nextDoubleFromTo(0,1)是什么呢
do?此外,Random#nextBoolean
有什么问题?0和1之间有多少个double
s?是的,对不起,我在测试时把它改成了0。我想看看是否能把它们都改成男性。我会把它改回原来的。@LinuxNoob1337自从原来的postorisF以来,OP已经更改了他的代码emale=随机>0.33
if (random > 0.33){
isFemale = true;
} else {
isFemale = false;
}
agent = new Agent(context, geography, random > 0.33, random);