如何使用 Java 来随机产生数据?
在写程序的时候常常会需要进行一些随机的动作,来增加程序的娱乐性和复杂性。在 Java 编程语言中,可以使用 Math 类别的 random 方法或是 Random 对象来达到取得随机数字的目的。取得随机数字之后还需要撰写一些程序来使用这个随机数,这类程序常会频繁地重复撰写,十分麻烦。为了精简程序,MagicRandom 实作好了一些常用的随机数据程序,只要使用一个静态方法就可以完成大部分的事。
MagicLen · https://magiclen.org
本站也提供繁體中文版。切換到繁體中文版
分类
Java 编程语言的教学与范例,包含语法观念、算法实作与桌面程序开发,并收录大量 Java 认证考试(OCAJP、OCPJP)的题目解析。
共 403 篇文章
在写程序的时候常常会需要进行一些随机的动作,来增加程序的娱乐性和复杂性。在 Java 编程语言中,可以使用 Math 类别的 random 方法或是 Random 对象来达到取得随机数字的目的。取得随机数字之后还需要撰写一些程序来使用这个随机数,这类程序常会频繁地重复撰写,十分麻烦。为了精简程序,MagicRandom 实作好了一些常用的随机数据程序,只要使用一个静态方法就可以完成大部分的事。
Java 5 之后加入了「泛型(generic)」,允许将对象的型态以参数的形式来定义。「?」是泛型的通配符,表示任意的对象型态,「?」还可以与「extends」和「super」两个关键字合用,至于它们的用法和差别在哪,将是本篇文章要探讨的部份。
快速选择(Quickselect)算法是利用快速排序(Quick Sort)算法,在排序串行的同时,选择出串行中第 K 小或是第 K 大的元素。若我们只想要从串行中找出一个第 K 小或是第 K 大元素,使用快速选择法会比使用快速排序法来得快很多,因为前者不需要把串行的排序完整做完,平均只需线性时间即可找到结果。
What is the result? A. {1007 = A, 1002 = C, 1001 = B, 1003 = B} B. {1001 = B, 1002 = C, 1003 = B, 1007 = A} C. {1002 = C, 1003 = B, 1007 = A} D. {1007 = A, 1001 = B, 1003 = B, 1002 = C}
Which code fragment should be inserted at line n1 to enable the code to print Rat Cat Lion Tiger? A. Arrays.sort(strArray, CheckClass::checkValue); B. Arrays.sort(strArray, (CheckClass::new) ::checkV...
What is the result? A. A file with the name log.txt is created in the /data directory and the content of the /data/december/log.txt file is copied to it. B. The program executes successfully and does...
Which modification enables the code fragment to print Happy Journey!? A. Replace line n1 with public void ride() throws FuelNotAvailException { B. Replace line n1 with protected void ride() throws...
What is the result? A. /app/sys/log /readme/server/exe B. /app/log/sys /server/exe/readme C. /app/./sys/log /readme D. /app/./sys/log /server/exe/readme
What is the result? A. [A Guide to Java Tour:3.0, Beginning with Java:2.0] B. [Beginning with Java:2.0, A Guide to Java Tour:3.0] C. A compilation error occurs because the Book class does not overrid...
What is the result? A. 0 B. 2 C. 3 D. 4 E. 5