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 ...
随机推荐
- python积累二:中文乱码解决方法
根据网上提供的解决方法:添加#coding=utf-8或# -*- coding: utf-8 -*- #coding=utf-8 print "还不行?" 执行结果:还是乱码!: ...
- Yii2增删改查
Controller <?php namespace frontend\controllers; use frontend\models\User; use yii\data\Paginatio ...
- huawei FPGA方案
https://indico.cern.ch/event/669648/contributions/2838181/attachments/1581893/2500031/Huawei_Cloud_F ...
- latex相关概念
关于Latex,收到网友的鼓励,决定好好整理下相关的信息. 在初次使用相关的程序时,遇到很多迷惑的概念,下面这篇帖子汇总得很详细. 关于latex各种概念与理解 帖子中提到了三个概念,引擎,宏集(即下 ...
- 201621123002《JAVA程序设计》第五周学习总结
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 接口 interface关键字 implements关键字 Comparable abstract Comparator 1.2 ...
- exception is java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make su re that file is correct.
spring cloud 项目使用maven 打包报错“No auto configuration classes found in META-INF/spring.factories” 在pom.x ...
- 3.在自己的bag上运行Cartographer ROS
1.验证自己的bag cartographer ROS提供了一个工具cartographer_rosbag_validate来自动分析包中的数据.在尝试调试cartographer之前运行这个工具. ...
- 第二次spring会议
今天所做之事: 我用C#用DelectText对行数进行了定义,刚开始写代码有点无从下手. 遇到的问题:刚开始用datagridView进行了文本的输入,但是它更适合EXCEL之类的数据计算不符合我们 ...
- Xpath在选择器中正确,在代码中返回的是空列表问题
一.问题: 在进行爬虫的时候我们会用到xpath解析html文件,但是会有一种情况就是在xpath选择器中可以使用,但是在代码中就无法使用的情况. 二.原因: 1.是元素中有tbody的原因,这个元素 ...
- Codeforces Round #547 (Div. 3) F 贪心 + 离散化
https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 ...