14-02 Java Math类,Random类,System类,BigDecimal类

时间:2023-01-28 07:40:07

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类的更多相关文章

  1. Math、Random、System、BigInteger、Date、DateFormat、Calendar类,正则表达式&lowbar;DAY14

    1:Math&大数据类四则运算 X abs(X x) double random()         产生随机数 double ceil(double a)   向上取整 double flo ...

  2. &lbrack;常用类&rsqb;Math、Random、System、BigInteger、BigDecimal

    Math类中的成员全是静态成员,构造方法是 私有的,以避免被创建对象 常用方法: int abs() double ceil() //向上取整 double floor() //向下取整 int ma ...

  3. java Math和Random和UUID

    Math类 public final class Math extends Object 以下X表示double,float,int, long abs(X x):求绝对值 max(X x1,X x2 ...

  4. Java基础 【Math、Random、System、BigInteger、BigDecimal、Date、Calendar等常用类的使用】

    学习的这几个类  是日常工作中经常要使用到的类 Math 类包含用于执行基本数序运算的方法,如初等指数.对数.平方根和 三角函数. 成员方法 1.public static int abs(int a ...

  5. Java基础教程——BigDecimal类

    BigDecimal类 float.double类型的数字在计算的时候,容易发生精度丢失. 使用java.math.BigDecimal类可以解决此类问题. 前面讲过Math类,现在的BigDecim ...

  6. 拯救你丢失的精度——BigInteger和BigDecimal类&lpar;入门&rpar;

    第三阶段 JAVA常见对象的学习 BigInteger和BigDecimal类 BigInteger类 (一) 构造方法: //针对超过整数范围的运算(整数最大值:2147483647) BigInt ...

  7. 正则表达式、Calendar类、SimpleDateFormat类、Date类、BigDecimal类、BigInteger类、System类、Random类、Math类&lpar;Java基础知识十四&rpar;

    1.正则表达式的概述和简单使用 * A:正则表达式(一个字符串,是规则)     * 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用. * B: ...

  8. java自学第4期——:Scanner类、匿名对象介绍、Random类、ArrayList集合、标准类格式、String类、static静态、Arrays工具类、Math类&lpar;1&rpar;

    一.Scanner类 1.api简介: 应用程序编程接口 2.Scanner类: 作用:获取键盘输入的数据 位置: java.util.Scanner. 使用:使用成员方法nextInt() 和 ne ...

  9. java 基本类型包装类,system类,Math类,Assrays类,大数据运算

    实现字符串与基本数据之间转换 将字符串转成基本数据类型方法 例如:将字符串转成成int类型 String str ="123"; int a =Integer.parseInt(s ...

随机推荐

  1. doT&period;js学习

    doT.js特点是快,小,无依赖其他插件.但是一般和jquery一起使用 官网:http://olado.github.io 使用方法:{{= }} for interpolation{{ }} fo ...

  2. win7 32 bit VS2012 OpenCV3&period;0配置

    今天看CPP基础,想起来之前在vs2012配置opencv3未成功,就忍不住再次配置一... 环境:win7 32bit vs2012 opencv3.0 主要参考这几篇博文:1,2,3 上面的博文已 ...

  3. Inside Flask - globals 全局变量(对象代理)

    Inside Flask - globals 全局变量(对象代理) 框架是一个容器,在框架内编程,一般是要遵守框架的约定和使用模式.通常这样的模式是 IoC,即由框架调用用户的代码,而不是用户调用框架 ...

  4. 《锋利的Jquery第二版》读书笔记 第一章

    按照书本介绍顺序整理jquery库相关的语法.要点. window.onload与$(document).ready()功能类似,前者需要所有资源加载完毕,且不能同时编写多个:后者加载完DOM结构即执 ...

  5. PKI 笔记

    PKI – Public Key Infrastructure , 通常翻译为公钥基础设施. PKI 安全平台提供的4个服务,来保证安全的数据,分别是: l  身份识别 l  数据保密 l  数据完整 ...

  6. ARMV8体系结构简介

    armv8 1.前言 本文的主要内容来源于ARMV8白皮书v5,对ARMV8做一个概述.包含如下的内容: 首先从背景谈起,讲述ARM的发展历程: 之后介绍ARMV8体系结构的基本特征: 介绍A64指令 ...

  7. pygame-KidsCanCode系列jumpy-part4-弹跳

    终于要到弹跳环节了,向上弹跳其实很简单,按下空格触发时,只要把y轴速度给一个向上的速度即可. Player类,新加一个jump()方法: def jump(self): self.vel.y = -2 ...

  8. python之文件操作read

    #open函数,该函数用于文件处理,文件操作一共就有三种方法,打开文件#关闭文件, #先来说下打开文件,打开文件的模式有下面几种# 1.r,只读模式 f = open('test.log','r',e ...

  9. 在虚拟机上安装linux系统

    1.安装linux服务器,内存4G,默认典型,next安装程序光盘影像文件,next选版本 2.6.x内核64位,next选择虚拟机位置(至少10G),next最大磁盘20G,选择单文件,next自定 ...

  10. Python之字符编码(Day10)

    1. python解释器执行py文件的原理 ,例如python test.py    第一阶段:python解释器启动,此时就相当于启动了一个文本编辑器 第二阶段:python解释器相当于文本编辑器, ...