• Create Account

### #Actualifthen

Posted 11 October 2012 - 10:58 AM

ByteTroll is right. Also, the reason why you get "tail" twice and "head" only once is that you have your brackets wrong. The second set of comparisons will be executed only if the first one rolls a tail. (Look at the indentation!)

By the way, the nextDouble() function returns 0 <= x < 1. That means that 0.5 should be a tail. You don't handle it in your if, so it won't print anything if exactly 0.5 is rolled (the probability of it, as we know, is 1 to DOUBLE_VALUES_BETWEEN_0_AND_1, so it probably won't happen in testing, but it is a logic error and you could lose points in the test or introduce nasty bugs if you work with production code!). You could modify your else if to "result > 0.5" or (since you are guaranteed that result won't be bigger than 1) just modify it to "else".

Or, you know, you could just use something like
void printRoll() {
if ((new Random().nextInt(2))==0)
else
System.out.println("TAILS");
}


### #1ifthen

Posted 11 October 2012 - 10:57 AM

ByteTroll is right. Also, the reason why you get "tail" twice and "head" only once is that you have your brackets wrong. The second set of comparisons will be executed only if the first one rolls a tail. (Look at the indentation!)

By the way, the nextDouble() function returns 0 <= x < 1. That means that 0.5 should be a tail. You don't handle it in your if, so it won't print anything if exactly 0.5 is rolled (the probability of it, as we know, is 1 to DOUBLE_VALUES_BETWEEN_0_AND_1, so it probably won't happen in testing, but it is a logic error and you could lose points in the test or introduce nasty bugs if you work with production code!). You could modify your else if to "result > 0.5" or (since you are guaranteed that result won't be bigger than 1) just modify it to "else".

Or, you know, you could just use something like
void printRoll() {
if (nextInt(2)==0)