您是否有一个想法,如何在对象不遵守条件之前生成随机对象?
我这样尝试过:
//Btw getTeam1() is a hashMap of Integer,Fighter (a type of objects that I use)
//randomIndex
Object randomName = this.getTeam1().keySet().toArray()[new Random().nextInt(this.getTeam1().keySet().toArray().length)];
while(this.getTeam1().get(randomName).dontVerifyMyConditionBlabla) {
randomName = this.getTeam1().keySet().toArray()[new Random().nextInt(this.getTeam1().keySet().toArray().length)];
}
但是显然,这听起来并不好。有什么建议吗?
最佳答案
尝试do-while
循环
verifyMyCondition(){
//proccess and return true or false
}
do
{
randomName = this.getTeam1().keySet().toArray()[new Random().nextInt(this.getTeam1().keySet().toArray().length)];
}while(verifyMyConditionthis.getTeam1().get(randomName)))