+ 1

Why does this problem print 0?

int a = 6; int b = (int)Math.random() * a; System.out.print(b);

29th Sep 2019, 3:14 AM
Juan Debenedetti
Juan Debenedetti - avatar
1 Resposta
0
You need to add brackets: int b = (int)(Math.random() * a); This calcs first Math.random() * a and then converts it to integer. Math.random() returns a double between 0 - 1. If you first convert it to integer it gives you always 0.
29th Sep 2019, 3:15 PM
Denise Roßberg
Denise Roßberg - avatar