Reputation: 23
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;
}
}
Hey everyone! Above is my code- for some reason regardless, it WILL ALWAYS, no matter what, always display the first option, which is "One of the computer's dice rolls was a 1, it lost all points for the round...". Even when I change the order of the statements, it still does this. Can someone please explain to me why this is happening?? Thanks!
Upvotes: 1
Views: 293
Reputation: 643
The problem is that you need to check if they are both 1, before checking if either of them are 1s. If we look at the code:
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++;
}
if:
Comproll1
= 1
Comproll2
= 1
You expect that it will go into the else if (Comproll1==1 && Comproll2==1)
however, if this is true than if (Comproll1==1 || Comproll2==1)
will always be true.
To fix this simply change the order of the if
s, like this:
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++;
}
Hope this helps :)
(Also you need to reroll the dice (as Elliott Frisch Said in his answer))
Upvotes: 1
Reputation: 201447
As far as I can tell, because you aren't re-rolling
int Comproll1= (int) (Math.random()*6+1);
int Comproll2= (int) (Math.random()*6+1);
while (m==1)
{
Should be
while (m==1)
{
int Comproll1= (int) (Math.random()*6+1);
int Comproll2= (int) (Math.random()*6+1);
Also, Java naming convention is camel case for variables (and starts with a lower case letter). So, Comproll1
might be compRoll1
. Finally, I personally prefer Random.nextInt()
and for 6 sided dice that might look like
Random rand = new Random();
while (m==1)
{
int compRoll1 = rand.nextInt(6) + 1;
int compRoll2 = rand.nextInt(6) + 1;
Edit Actually, you also need to reverse the order of your tests. Because if either is true then it will never be possible that the test for both being true will be entered.
if (Comproll1==1 || Comproll2==1) {
// Here.
}else if (Comproll1==1 && Comproll2==1) {
// Will never enter here.
}
Switch the order to,
if (Comproll1==1 && Comproll2==1) {
// Both.
}else if (Comproll1==1 || Comproll2==1) {
// Either.
}
Upvotes: 3
Reputation: 963
Try changing the order of your if statements. Logically, if one of the two comparisons are true, the first statement will execute. In the case that the second conditions else if (Comproll1==1 && Comproll2==1)
are true, the first conditions if (Comproll1==1 || Comproll2==1)
will also be true.
Since you've chained the if statements in an if-else-if fashion, the first if statement to equate to true will execute.
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;
}
Upvotes: 0