Danh mục

Java Programming for absolute beginner- P5

Số trang: 20      Loại file: pdf      Dung lượng: 485.84 KB      Lượt xem: 13      Lượt tải: 0    
10.10.2023

Hỗ trợ phí lưu trữ khi tải xuống: 17,000 VND Tải xuống file đầy đủ (20 trang) 0

Báo xấu

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Java Programming for absolute beginner- P5:Hello and welcome to Java Programming for the Absolute Beginner. You probablyalready have a good understanding of how to use your computer.These days it’s hard to find someone who doesn’t, given the importanceof computers in today’s world. Learning to control your computer intimatelyis what will separate you from the pack! By reading this book, you learnhow to accomplish just that through the magic of programming.
Nội dung trích xuất từ tài liệu:
Java Programming for absolute beginner- P5 JavaProgAbsBeg-03.qxd 2/25/03 8:49 AM Page 58 58 Java Programming for the Absolute Beginner FIGURE 3.2 The NumberMaker displays random numbers generated by the Math.random() method. The java.util.Random Class Another way that you can generate random numbers is by using the Random class in the java.util package. The Random class offers different methods for different data types. Specifically, it can generate random booleans, doubles, floats, ints, and longs. Refer to Table 3.1 for a list of these methods. TA B L E 3 . 1 SOME JAVA . UTIL .R ANDOM METHODS Method Description boolean nextBoolean() Randomly returns either true or false boolean values. double nextDouble() Returns a random double value ranging from 0.0 (inclusive) to 1.0 (exclusive). float nextFloat() Returns a random float value ranging from 0.0 (inclusive) to 1.0 (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). In order to call one of the methods in Table 3.1, you need to create a new Random object first, and then use that object to call the desired method. The Number- MakerUtil application demonstrates how this is done. Take a look at the source code: /* * NumberMakerUtil * Uses java.util.Random to generate random numbers */ TEAM LinG - Live, Informative, Non-cost and Genuine!Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. JavaProgAbsBeg-03.qxd 2/25/03 8:49 AM Page 59 59 import java.util.Random; Chapter 3 public class NumberMakerUtil { public static void main(String args[]) { Random rand = new Random(); System.out.println(“Random Integers:”); System.out.println(rand.nextInt() + “, “ The Fortune Teller: Random Numbers, Conditionals, and Arrays + rand.nextInt() + “, “ + rand.nextInt() ...

Tài liệu được xem nhiều: