这是确切的代码,然后我有一个案例0的开关:和案例1:似乎案件1:每次出来,我想有一个50/50的机会0或1出来是这个正确的方法或我应该使用1.5或这究竟是如何工作的?
talka = (int)(Math.random() * 1);
switch(talka)
{
case 0:
{
talk.setAnimationListener(this);
talk.playtimes(1,24);
startService(new Intent(this, love1.class));
break;
}
case 1:
{
talk.setAnimationListener(this);
talk.playtimes(1,12);
startService(new Intent(this, love2.class));
break;
}
}
Run Code Online (Sandbox Code Playgroud)
这总是向下舍入.
talka = (int)(Math.random() * 1); // between 0 and 0
Run Code Online (Sandbox Code Playgroud)
你的意图也许是
talka = (int)(Math.random() * 2); // between 0 and 1
Run Code Online (Sandbox Code Playgroud)
但是,使用Math.random()得到一位效率非常低.
如果你使用随机随机
talka = random.nextInt(2);
Run Code Online (Sandbox Code Playgroud)
甚至更好
talk.setAnimationListener(this);
if (random.nextBoolean()) {
talk.playtimes(1,24);
startService(new Intent(this, love1.class));
} else {
talk.playtimes(1,12);
startService(new Intent(this, love2.class));
}
Run Code Online (Sandbox Code Playgroud)