int Comproll1= (int) (Math.random()*6+1);
int Comproll2= (int) (Math.random()*6+1);
while (m==1)
{
{
if (Comproll1==1 || Comproll2==1)
{
System.out.println("One of the computer's dice rolls was a 1, it lost all the points for the round & it is now your turn!");
cr= cr-cr;
m++;
}
else if (Comproll1==1 && Comproll2==1)
{
System.out.println("The Computer rolled 2 1's, their total number of points is now 0 & it is now your turn!");
cp=cp-cp;
m++;
}
else
{
cr= Comproll1+Comproll2;
cp= cp+cr;
}
}
嘿大家好!以上是我的代码 - 无论如何,无论如何,无论如何,总是会显示第一个选项,即“计算机的一个掷骰子是1,它在一轮中失去了所有点...”。即使我改变陈述的顺序,它仍然会这样做。有人可以向我解释为什么会发生这种情况吗?谢谢!Math.random if语句错误
首先,'int Comproll1 =(int)(Math.random()* 6 + 1)'应该是5 + 1。如果随机返回6呢?然后你将有一个7的骰子。 – 2014-10-11 23:22:47
@BoratSagdiyev'Math。random()'返回一个浮点数'<1',所以'Math.random()* 6 + 1'的返回值永远不会高于'6.99 ...',因此如果转换为整数,则返回6。 – lexith 2014-10-11 23:25:08
@BoratSagdiyev事实并非如此。 'Math.random()'的结果保证<1,因此,'Math.random()* 6'将始终为<6,并且'Math.random()* 6 + 1'将始终为<7 。对'int'的转换使得6成为最大值。 – ApproachingDarknessFish 2014-10-11 23:25:29