Math.random if语句错误

时间:2014-10-11 23:19:25

标签: java

    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,它失去了所有点数圆...&#34 ;.即使我改变了语句的顺序,它仍然会这样做。有人可以向我解释为什么会这样吗?谢谢!

3 个答案:

答案 0 :(得分:3)

据我所知,因为你没有重新滚动

int Comproll1= (int) (Math.random()*6+1);
int Comproll2= (int) (Math.random()*6+1);
while (m==1)
{

应该是

while (m==1)
{
  int Comproll1= (int) (Math.random()*6+1);
  int Comproll2= (int) (Math.random()*6+1);

此外,Java命名约定是变量的驼峰式(并以小写字母开头)。因此,Comproll1可能是compRoll1。最后,我个人更喜欢Random.nextInt()和6面骰子,可能看起来像

Random rand = new Random();
while (m==1)
{
  int compRoll1 = rand.nextInt(6) + 1;
  int compRoll2 = rand.nextInt(6) + 1;

编辑实际上,您还需要撤消测试的顺序。因为如果其中任何一个为真,那么将永远不可能输入两个为真的测试。

if (Comproll1==1 || Comproll2==1) {
  // Here.
}else if (Comproll1==1 && Comproll2==1) {
  // Will never enter here.
}

将订单切换到,

if (Comproll1==1 && Comproll2==1) {
  // Both.
}else if (Comproll1==1 || Comproll2==1) {
  // Either.
}

答案 1 :(得分:1)

问题是你需要检查它们是否都是1,然后检查它们中的任何一个是否为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++;
}

如果:

Comproll1 = 1

Comproll2 = 1

您希望它会进入else if (Comproll1==1 && Comproll2==1)但是,如果这是真的,那么if (Comproll1==1 || Comproll2==1)始终为真。

要解决此问题,只需更改if s的顺序,如下所示:

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 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++;
}

希望这会有所帮助:)

(你也需要重新掷骰子(正如Elliott Frisch Said在他的回答中所说))

答案 2 :(得分:0)

尝试更改if语句的顺序。从逻辑上讲,如果两个比较中的一个为真,则第一个语句将执行。如果第二个条件else if (Comproll1==1 && Comproll2==1)为真,则第一个条件if (Comproll1==1 || Comproll2==1)也将为真。

由于您以if-else-if方式链接if语句,因此将执行等于true的第一个if语句。

    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 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 
    {
        cr= Comproll1+Comproll2;
        cp= cp+cr;
    }