Consider the following program segment:

boolean finished = false;

do {

double d = Math.random();

if(d > .5) finished = true;

System.out.println(d);

} while (!finished);



Math.random( ) generates a decimal value which is less than 1. What should be the value of d so that the loop ends?

Respuesta :

“Math.random()” in java returns a value between 0.0 and 1.0. Since the number generated is random in nature, the user does not have a control on it.

According to the program, a number is obtained using random() and the number is checked whether it is greater than 0.5. If so, then the loop is over, the execution stops. If it is less than .5 or equal to .5, the number will get printed. So the loop ends when the value is >.5 (greater than 0.5).

ACCESS MORE