jtcy
asked on
initialise var
How can i initialise an integer variable to be 1-10?
No, that delivers 0 - 10, correct is:
int i = rand.nextInt(10) + 1;
;JOOP!
int i = rand.nextInt(10) + 1;
;JOOP!
ASKER
would this work?
int ballToGoal = (int) (Math.random () * 10) ;
int ballToGoal = (int) (Math.random () * 10) ;
No jtcy, that produces 0 - 9
;JOOP!
;JOOP!
>> int ballToGoal = (int) (Math.random () * 10) ;
int ballToGoal = ( int ) Math.round ( ( Math.random () * 10 ) ) ; could work. Or: int ballToGoal = ( int ) ( Math.random () * 11 ) ;
- because random () returns double values greater than or equal to 0.0 and lesser than 1.0. So when you take the ( int ) part of Math.random () * 10, it will always lesser than or equal to 9 (even 9.9 would be converted to 9, that way).
int ballToGoal = ( int ) Math.round ( ( Math.random () * 10 ) ) ; could work. Or: int ballToGoal = ( int ) ( Math.random () * 11 ) ;
- because random () returns double values greater than or equal to 0.0 and lesser than 1.0. So when you take the ( int ) part of Math.random () * 10, it will always lesser than or equal to 9 (even 9.9 would be converted to 9, that way).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Like I said.
;JOOP!
;JOOP!
Yes - missed that. But it's better if he has the right code
int i = rand.nextInt(11);
// the 11 means (10 + 1) beacuse the parameter 11 is exclusive.