JAVASE基础模块二十(Math System 常用类)
2020-08-01 10:36
771 查看
JAVASE基础模块二十(Math System 常用类)
Math
-
常用数学方法
public class Mat { public static void main(String[] args) { System.out.println(Math.PI);//圆周率 System.out.println(Math.E);//自然对数e System.out.println(Math.abs(-1));//相反数 System.out.println(Math.ceil(3.1));//向上取整 System.out.println(Math.floor(3.4));//向下取整 System.out.println(Math.random());//随机数 System.out.println(Math.max(66, 77));//最大值 System.out.println(Math.min(3, 7));//最小值 System.out.println(Math.pow(3, 3));//3的3次方 System.out.println(Math.sqrt(9));//开根号9 System.out.println(Math.round(3.6));//四舍五入 System.out.println(Math.pow(8, 1/3.0));//开三次根号8 即8的1/3次方 } } 运行结果: 3.141592653589793 2.718281828459045 1 4.0 3.0 0.631757479119988 77 3 27.0 3.0 4 2.0 进程已结束,退出代码0
-
Random 可以生产一些随机数据
-
Random() 创建一个新的随机数生成器
import java.util.Random; public class SuiJi { public static void main(String[] args) { Random s = new Random(); for (int i = 0; i < 10; i++) { //生成10以内的随机数 不包括10 int n = s.nextInt(); int n1 = s.nextInt(10); System.out.print(n1 + "\t"); } System.out.println(); //生成随机小数 for (int i = 0; i < 10; i++) { double c = s.nextDouble(10); System.out.print(c + "\t"); } System.out.println(); //生成随机boolean结果 for (int i = 0; i < 10; i++) { boolean d = s.nextBoolean(); System.out.print(d + "\t"); } } } 运行结果: 0 6 0 4 5 1 8 0 4 3 0.9612622209326054 0.028419643402946226 0.970400836746776 0.650129681131199 0.0464290713465646 0.7594233858710998 0.05472876539698879 0.9022535461223241 0.37485397139752463 0.7533694820433411 false true true false false false true false false true Process finished with exit code 0
-
Random( long seed) 使用单个 long 种子创建一个新的随机数生成器
import java.util.Random; public class Kcan { public static void main(String[] args) { //不给种子 每次结果不一样 Random s = new Random(); for (int i = 0; i < 10; i++) { int ii = s.nextInt(10); System.out.print(ii + "\t"); } System.out.println(); //给了种子就会算出一些随机数 每次运行的结果一样 Random s1 = new Random(3l); for (int i1 = 0; i1 < 10; i1++) { int ii1 = s1.nextInt(10); System.out.print(ii1 + "\t"); } } } 第一次运行结果: 9 1 4 2 8 5 0 6 5 0 4 0 0 1 8 2 9 4 9 1 Process finished with exit code 0 第二次运行结果: 6 1 7 6 9 4 2 6 4 3 4 0 0 1 8 2 9 4 9 1 Process finished with exit code 0 第三次运行结果: 1 1 1 8 1 3 9 0 8 7 4 0 0 1 8 2 9 4 9 1 Process finished with exit code 0
void nextBytes ( byte[] bytes) 生成随机字节并将其置于用户提供的 byte 数组中
import java.util.Arrays; import java.util.Random; public class DD { public static void main(String[] args) { Random random = new Random(); byte[] bytes = new byte[10]; random.nextBytes(bytes); System.out.println(Arrays.toString(bytes)); } } 运行结果: [119, 98, 114, -30, 74, 49, -71, 83, 100, 42] Process finished with exit code 0
System
-
System 类包含一些有用的类字段和方法 它不能被实例化
-
in public static final InputStream in“标准”输入流。此流已打开并准备提供输入数据。通常,此流对应于键盘输入或者由主机环境或用户指定的另一个输入源
-
out public static final PrintStream out“标准”输出流。此流已打开并准备接受输出数据。通常,此流对应于显示器输出或者由主机环境或用户指定的另一个输出目标
-
对于简单独立的 Java 应用程序,编写一行输出数据的典型方式是:System.out.println(data)
public class MyTest { public static void main(String[] args) { PrintStream out = System.out; out.println("abc"); System.out.println(200); System.out.println("============================="); System.err.println("错误信息"); } } 运行结果: abc 200 ============================= 错误信息(红色) Process finished with exit code 0
-
exit(0); 退出虚拟机 0 正常退出 非0 强制退出
public class Syst { public static void main(String[] args) { System.out.println("123456"); System.out.println("123456"); System.out.println("123456"); System.exit(0); System.out.println("123456"); System.out.println("123456"); System.out.println("123456"); } } 运行结果: 123456 123456 123456 进程已结束,退出代码0
-
System.currentTimeMillis(); 获取当前系统时间的毫秒值(获取从 1970 01-01 00:00:00 到现在的间隔的 毫秒值)
public class HaoMiao { public static void main(String[] args) { long timeMillis=System.currentTimeMillis(); System.out.println(timeMillis); //测试循环的耗时 for (int i = 0; i <100 ; i++) { System.out.print(i+"\t"); } long timeMillis1=System.currentTimeMillis(); System.out.println(timeMillis1-timeMillis); } } 运行结果: 1596249190893 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 时间:15 Process finished with exit code 0
-
垃圾回收器 System.gc(); Runtime.getRuntime().gc(); 回收垃圾
public class LaJi { public static void main(String[] args) { //运行垃圾回收器 回收垃圾 System.gc(); Runtime.getRuntime().gc(); //ctrl+alt+空格 System.out.println(System.getProperty("java.version")); System.out.println(System.getProperty("java.class.path")); } } 运行结果: 13.0.2 D:\IDEA\IdeaProjects\System\out\production\System Process finished with exit code 0
待续…
相关文章推荐
- Py修行路 python基础 (二十)模块 time模块,random模块,hashlib模块,OS及sys模块
- 黑马程序员_JavaSE基础知识总结二十:课堂上关于集合的几个超经典案例(结课前集合知识温故)
- Python基础(time模块,datetime模块)
- Angular基础学习二(包含了组件,服务,模块,组件通信)
- rootkit:在隐藏模块的基础上隐藏进程
- node.js的http模块的基础 学到的东西
- JAVASE基础回顾总结(一)
- python基础===中文手册,可查询各个模块
- javaSE基础06之内部类
- linux设备驱动归纳总结(二):模块的相关基础概念
- 【FPGA】【Verilog】【基础模块】分频器
- python基础教程之数字处理(math)模块详解
- Nginx基础. 开发HTTP过滤模块
- JavaSE基础之Java语言(JDK中的命令行工具)
- ELK日志分析Elasticsearch模块——语法基础CRUD
- Java 基础入门随笔(2) JavaSE版——关键字、进制转换、类型转换
- [Java 并发] Java并发编程实践 思维导图 - 第五章 基础构建模块
- python基础学习笔记<函数式编程与模块>
- Java 基础入门随笔(9) JavaSE版——文档注释
- JavaSE基础知识框架及概述