2014-10-11 88 views
1
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语句错误

+0

首先,'int Comproll1 =(int)(Math.random()* 6 + 1)'应该是5 + 1。如果随机返回6呢?然后你将有一个7的骰子。 – 2014-10-11 23:22:47

+2

@BoratSagdiyev'Math。random()'返回一个浮点数'<1',所以'Math.random()* 6 + 1'的返回值永远不会高于'6.99 ...',因此如果转换为整数,则返回6。 – lexith 2014-10-11 23:25:08

+2

@BoratSagdiyev事实并非如此。 'Math.random()'的结果保证<1,因此,'Math.random()* 6'将始终为<6,并且'Math.random()* 6 + 1'将始终为<7 。对'int'的转换使得6成为最大值。 – ApproachingDarknessFish 2014-10-11 23:25:29

回答

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

是不是因为他实际上是在第一个if语句中对两个条件进行ORing?看第二个条件,他是ANDing,所以假设这是真的,第一个if语句也应该是真的。编辑:这可能是两个原因。 – adchilds 2014-10-11 23:24:16

0

试着改变你的if语句的顺序。逻辑上,如果两个比较中的一个是真的,则第一个语句将执行。在第二条件else if (Comproll1==1 && Comproll2==1)为真的情况下,第一条件if (Comproll1==1 || Comproll2==1)也将成立。

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

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

希望这有助于:)

(你也需要重掷色子(如埃利奥特·弗里施说,他的回答))