Monday, 15 February 2010

java - Using a while loop to generate random numbers until a certain number is reached -



java - Using a while loop to generate random numbers until a certain number is reached -

i'm learning while loops. in java class i'm trying modify programme utilize basic while loop generate random numbers until number reached. in particular case want print until goes below .0001. i've gotten myself confused while trying , not getting output. i'm looking hints or tips might have help me along or help farther understanding. here's have code far:

import java.util.random; public class randomnumbers { public static void main(string[] args) { random rand = new random(); double val=1; while(val<.0001){ val=rand.nextdouble(); system.out.println(val); } } }

simple logic error. based on current code while loop never run because val<.0001 false (1 > .0001). need modify line this:

while(val > 0.0001){

also it's improve write decimals 0 in front end of . improved readability.

java while-loop

No comments:

Post a Comment