Math
类
● java.lang.Math
提供了一系列静态方法用于科学计算;其方法的参数和
返回值类型一般为
double
型。
abs
绝对值
sqrt
平方根
pow(double a, double b) a
的
b
次幂
max(double a, double b)
min(double a, double b)
random()
返回
0.0
到
1.0
的随机数
long round(double a) double
型的数据
a
转换为
long
型(四舍五入)
public class MathDemo {
public static void main(String[] args) {
System.out.println(Math.PI);
System.out.println(Math.abs(-1));
System.out.println(Math.sqrt(9));
System.out.println(Math.random());//返回0-1之间随机数
System.out.println(Math.floor(9.9));//向下取整
System.out.println(Math.ceil(9.1));//向上取整
System.out.println(Math.round(9.6));//四舍五入
}
}
Random
类
●
Random类概述
此类用于产生随机数
●
构造方法
public Random()
成员方法
public int nextInt()
public int nextInt(int n)
public class RandomDemo {
public static void main(String[] args) {
Random r = new Random();
//System.out.println(r.nextInt());//在int的范围内随机取出
//System.out.println(r.nextInt(4));//0--<4 在指定的区间内随机取出
//System.out.println(r.nextBoolean());
byte[] b = new byte[5];//随机取出 数组.length个随机数
r.nextBytes(b);
System.out.println(Arrays.toString(b));
}
}
System
类
● System
类概述
System
类包含一些有用的类字段和方法。它不能被实例化。
●
成员方法
public static void exit(int status)
public static long currentTimeMillis()
public class SystemDemo {
public static void main(String[] args) {
//public final static PrintStream out = null;
// System.out.print(1);
//1637483235781 是自1970.1.1 0:0:0 --- 程序运行时的那一刻的时间差(毫秒为单位)
//Long s = System.currentTimeMillis();
//System.out.println(s);
//System.exit(0); 退出虚拟机
//System.gc(); 垃圾回收 一般程序中不使用
//System.out.println("qqqqqqqqqqq");
//arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
int a[] = {1,2,3,4,5};
int b[] = new int[10];
System.arraycopy(a,1,b,0,4); //系统中提供的最底层的数组内容复制方法
System.out.println(Arrays.toString(b));
}
}