14-02 Java Math类,Random类,System类,BigDecimal类
Math类
Math的方法
package cn.itcast_01; /*
* Math:用于数学运算的类。
* 成员变量:
* public static final double PI
* public static final double E
* 成员方法:
* public static int abs(int a):绝对值
* public static double ceil(double a):向上取整
* public static double floor(double a):向下取整
* public static int max(int a,int b):最大值 (min自学)
* public static double pow(double a,double b):a的b次幂
* public static double random():随机数 [0.0,1.0)
* public static int round(float a) 四舍五入(参数为double的自学)
* public static double sqrt(double a):正平方根
*/
public class MathDemo {
public static void main(String[] args) {
// public static final double PI
System.out.println("PI:" + Math.PI);
// public static final double E
System.out.println("E:" + Math.E);
System.out.println("--------------"); // public static int abs(int a):绝对值
System.out.println("abs:" + Math.abs(10));
System.out.println("abs:" + Math.abs(-10));
System.out.println("--------------"); // public static double ceil(double a):向上取整
System.out.println("ceil:" + Math.ceil(12.34));
System.out.println("ceil:" + Math.ceil(12.56));
System.out.println("--------------"); // public static double floor(double a):向下取整
System.out.println("floor:" + Math.floor(12.34));
System.out.println("floor:" + Math.floor(12.56));
System.out.println("--------------"); // public static int max(int a,int b):最大值
System.out.println("max:" + Math.max(12, 23));
// 需求:我要获取三个数据中的最大值
// 方法的嵌套调用
System.out.println("max:" + Math.max(Math.max(12, 23), 18));
// 需求:我要获取四个数据中的最大值
System.out.println("max:"
+ Math.max(Math.max(12, 78), Math.max(34, 56)));
System.out.println("--------------"); // public static double pow(double a,double b):a的b次幂
System.out.println("pow:" + Math.pow(2, 3));
System.out.println("--------------"); // public static double random():随机数 [0.0,1.0)
System.out.println("random:" + Math.random());
// 获取一个1-100之间的随机数
System.out.println("random:" + ((int) (Math.random() * 100) + 1));
System.out.println("--------------"); // public static int round(float a) 四舍五入(参数为double的自学)
System.out.println("round:" + Math.round(12.34f));
System.out.println("round:" + Math.round(12.56f));
System.out.println("--------------"); //public static double sqrt(double a):正平方根
System.out.println("sqrt:"+Math.sqrt(4));
}
}
Math.random()
package cn.itcast_02; import java.util.Scanner; /*
* 需求:请设计一个方法,可以实现获取任意范围内的随机数。
*
* 分析:
* A:键盘录入两个数据。
* int strat;
* int end;
* B:想办法获取在start到end之间的随机数
* 我写一个功能实现这个效果,得到一个随机数。(int)
* C:输出这个随机数
*/
public class MathDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入开始数:");
int start = sc.nextInt();
System.out.println("请输入结束数:");
int end = sc.nextInt(); for (int x = 0; x < 100; x++) {
// 调用功能
int num = getRandom(start, end);
// 输出结果
System.out.println(num);
}
} /*
* 写一个功能 两个明确: 返回值类型:int 参数列表:int start,int end
*/
public static int getRandom(int start, int end) { int number = (int) (Math.random() * (end - start + 1)) + start;
return number;
}
}
Random类
package cn.itcast_01; import java.util.Random; /*
* Random:产生随机数的类
*
* 构造方法:
* public Random():没有给种子,用的是默认种子,是当前时间的毫秒值
* public Random(long seed):给出指定的种子
*
* 给定种子后,每次得到的随机数是相同的。
*
* 成员方法:
* public int nextInt():返回的是int范围内的随机数
* public int nextInt(int n):返回的是[0,n)范围的内随机数
*/
public class RandomDemo {
public static void main(String[] args) {
// 创建对象
// Random r = new Random();
Random r = new Random(1111); for (int x = 0; x < 10; x++) {
// int num = r.nextInt();
int num = r.nextInt(100) + 1;
System.out.println(num);
}
}
}
System类
系统类,提供了一些有用的字段和方法
运行垃圾回收器
package cn.itcast_01; public class Person {
private String name;
private int age; public Person() {
super();
} public Person(String name, int age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
} @Override
protected void finalize() throws Throwable {
System.out.println("当前的对象被回收了" + this);
super.finalize();
} }
package cn.itcast_01; /*
* System类包含一些有用的类字段和方法。它不能被实例化。
*
* 方法:
* public static void gc():运行垃圾回收器。
* public static void exit(int status)
* public static long currentTimeMillis()
* public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
*/
public class SystemDemo {
public static void main(String[] args) {
Person p = new Person("赵雅芝", 60);
System.out.println(p); p = null; // 让p不再指定堆内存
System.gc();
}
}
退出jvm,获取当前时间的毫秒值
package cn.itcast_02; /*
* System类包含一些有用的类字段和方法。它不能被实例化。
*
* 方法:
* public static void gc():运行垃圾回收器。
* public static void exit(int status):终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。
* public static long currentTimeMillis():返回以毫秒为单位的当前时间
* public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
*/
public class SystemDemo {
public static void main(String[] args) {
// System.out.println("我们喜欢林青霞(东方不败)");
// System.exit(0);
// System.out.println("我们也喜欢赵雅芝(白娘子)"); // System.out.println(System.currentTimeMillis()); // 单独得到这样的实际目前对我们来说意义不大
// 那么,它到底有什么作用呢?
// 要求:请大家给我统计这段程序的运行时间
long start = System.currentTimeMillis();
for (int x = 0; x < 100000; x++) {
System.out.println("hello" + x);
}
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
}
}
数组复制
package cn.itcast_03; import java.util.Arrays; /*
* System类包含一些有用的类字段和方法。它不能被实例化。
*
* 方法:
* public static void gc():运行垃圾回收器。
* public static void exit(int status):终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。
* public static long currentTimeMillis():返回以毫秒为单位的当前时间
* public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
* 从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
*/
public class SystemDemo {
public static void main(String[] args) {
// 定义数组
int[] arr = { 11, 22, 33, 44, 55 };
int[] arr2 = { 6, 7, 8, 9, 10 }; // 请大家看这个代码的意思
System.arraycopy(arr, 2, arr2, 1, 2); System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
}
}
14-02 Java Math类,Random类,System类,BigDecimal类的更多相关文章
- Math、Random、System、BigInteger、Date、DateFormat、Calendar类,正则表达式_DAY14
1:Math&大数据类四则运算 X abs(X x) double random() 产生随机数 double ceil(double a) 向上取整 double flo ...
- [常用类]Math、Random、System、BigInteger、BigDecimal
Math类中的成员全是静态成员,构造方法是 私有的,以避免被创建对象 常用方法: int abs() double ceil() //向上取整 double floor() //向下取整 int ma ...
- java Math和Random和UUID
Math类 public final class Math extends Object 以下X表示double,float,int, long abs(X x):求绝对值 max(X x1,X x2 ...
- Java基础 【Math、Random、System、BigInteger、BigDecimal、Date、Calendar等常用类的使用】
学习的这几个类 是日常工作中经常要使用到的类 Math 类包含用于执行基本数序运算的方法,如初等指数.对数.平方根和 三角函数. 成员方法 1.public static int abs(int a ...
- Java基础教程——BigDecimal类
BigDecimal类 float.double类型的数字在计算的时候,容易发生精度丢失. 使用java.math.BigDecimal类可以解决此类问题. 前面讲过Math类,现在的BigDecim ...
- 拯救你丢失的精度——BigInteger和BigDecimal类(入门)
第三阶段 JAVA常见对象的学习 BigInteger和BigDecimal类 BigInteger类 (一) 构造方法: //针对超过整数范围的运算(整数最大值:2147483647) BigInt ...
- 正则表达式、Calendar类、SimpleDateFormat类、Date类、BigDecimal类、BigInteger类、System类、Random类、Math类(Java基础知识十四)
1.正则表达式的概述和简单使用 * A:正则表达式(一个字符串,是规则) * 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用. * B: ...
- java自学第4期——:Scanner类、匿名对象介绍、Random类、ArrayList集合、标准类格式、String类、static静态、Arrays工具类、Math类(1)
一.Scanner类 1.api简介: 应用程序编程接口 2.Scanner类: 作用:获取键盘输入的数据 位置: java.util.Scanner. 使用:使用成员方法nextInt() 和 ne ...
- java 基本类型包装类,system类,Math类,Assrays类,大数据运算
实现字符串与基本数据之间转换 将字符串转成基本数据类型方法 例如:将字符串转成成int类型 String str ="123"; int a =Integer.parseInt(s ...
随机推荐
- PHP牛牛游戏算法
<?php namespace frontend\business; class NiuNiuGameHelper { /** * @param $card * @return int 结果 - ...
- layui模板和jfinal混合使用注意
<!-- 列表信息展示 --> <div class="layui-row"> <table class="layui-table" ...
- java项目测试或者不使用request,如何获取webroot路径
1.使用jdk中的方法,然后根据项目编译后的文件存在的位置,获取到classes目录,然后向上级查询获取String path = EngineTest.class.getResource(" ...
- web项目局部打印
window.print()方法是打印整个body,若想打印局部区域,网上出现了各种解决办法,我觉得都挺好的.我最推荐jquery.PrintArea.js插件形式 点击上述链接首先下载下来,我的是版 ...
- [leetcode]55. Jump Game青蛙跳(能否跳到终点)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- iOS 拨打电话三种方式
,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示 NSMutableString * str=[[NSMutableString alloc] initWithFo ...
- redis持久化详述
本来打算根据自己搜索的一些文章写些总结,后来发现了一篇好文,这里转载下,在自己博客里面记录下. 原文链接:https://www.cnblogs.com/kismetv/p/9137897.html ...
- 将小程序的logo换成属于自己的头像
$logo = file_get_contents(IMG_URL.$logo); //根据头像的路径 $logo = yuanImg1($logo); //将头像改为圆形 $data = file_ ...
- JQUERY-定义-查找
正课: 1. 什么是jQuery 2. 如何使用jQuery 3. 查找 1. 什么是jQuery 第三方开发的 执行DOM操作的 极简化的 函数库 第三方: 下载 执行DOM操作: 学习jQuery ...
- VM下载安装
VM下载 VM是一款收费软件,要找有密钥的下载. 我的网盘 > 软件 > 常用电脑工具 > VM VM安装 参考链接中的安装步骤 http://blog.java1234.com/b ...