Problem:
Return the sum of two 6-sided dice rolls, each in the range 1..6. However, if noDoubles is true, if the two dice show the same value, increment one die to the next value, wrapping around to 1 if its value was 6.
withoutDoubles(2, 3, true) → 5
withoutDoubles(3, 3, true) → 7
withoutDoubles(3, 3, false) → 6
Solution:
public int withoutDoubles(int die1, int die2, boolean noDoubles) { if(noDoubles) { if (die1 == die2) { if (die1 != 6) return die1 + 1 + die2; else return 1 + die2; } } return die1 + die2; }
if (noDoubles&&die1==die2) return (die1!= 6)?die1+1+die2:1+die2;
ReplyDeletereturn die1 + die2;
if (noDoubles) {
ReplyDeleteif (die1 == 6)
die1 = 1;
else if (die1 == die2)
die1 += 1;
}
return die1 + die2;
private final int withoutDoubles(int die1, int die2, boolean noDoubles) {
ReplyDeleteif (noDoubles && die1 == die2){
if(die1 == 6){
return 7;
}
return die1+ die2 + 1;
}
return die1 + die2;
}
public int withoutDoubles(int die1, int die2, boolean noDoubles) {
ReplyDeleteint sum = die1 + die2;
if (sum <= 6 && noDoubles == false)
return sum;
else if (die1 == 6 && die2 == 6 && noDoubles == true)
return die1 + 1;
else if (die1 == die2 && noDoubles == true)
return sum + 1;
else
return sum;
}
public int withoutDoubles(int die1, int die2, boolean noDoubles) {
Deleteint sum = die1 + die2;
if (die1 == 6 && die2 == 6 && noDoubles == true)
return die1 + 1;
else if (die1 == die2 && noDoubles == true)
return sum + 1;
else
return sum;
}
int result=0;
ReplyDeleteif(noDoubles&&die1==die2){
if(die1!=6) result= die1+die2+1;
else if(die1==6) result= 7;
}else{
result= die1+die2;
}
return result;
public int withoutDoubles(int die1, int die2, boolean noDoubles) {
ReplyDeleteif (noDoubles && die1 == 6 && die2 == 6)
return die1 + 1;
else if (noDoubles && die1 == die2)
return die1+die2+1;
else return die1+die2;
}