TAILIEUCHUNG - Java Programming for absolute beginner- P5
Java Programming for absolute beginner- P5:Hello and welcome to Java Programming for the Absolute Beginner. You probably already have a good understanding of how to use your computer. These days it’s hard to find someone who doesn’t, given the importance of computers in today’s world. Learning to control your computer intimately is what will separate you from the pack! By reading this book, you learn how to accomplish just that through the magic of programming. | 2 25 03 8 49 AM Page 58 FIGURE The NumberMaker displays random numbers generated by the method. The Class Another way that you can generate random numbers is by using the Random class in the package. The Random class offers different methods for different data types. Specifically it can generate random booleans doubles floats i nts and longs. Refer to Table for a list of these methods. Table Some Methods 4 Method Description boolean nextBoolean Randomly returns either true or false boolean values. double nextDouble Returns a random double value ranging from inclusive to exclusive . float nextFloat Returns a random float value ranging from inclusive to exclusive . int nextInt Returns a random int value all 232 values are possible . int nextInt int n Returns a random int value ranging from 0 inclusive to n exclusive . long nextLong Returns a random long value all 264 values are possible . X J In order to call one of the methods in Table you need to create a new Random object first and then use that object to call the desired method. The NumberMakerUtil application demonstrates how this is done. Take a look at the source code NumberMakerUtil Uses to generate random numbers team Line - live informative Non-cost and Genuine Please purchase PDF Split-Merge on to remove this watermark. 2 25 03 8 49 AM Page 59 import public class NumberMakerUtil public static void main String args Random rand new Random Random Integers int iLimit 11 nRandom Integers between 0 and 10 iLimit iLimit iLimit nRandom Floats nRandom Booleans .
đang nạp các trang xem trước