Java学习提要——Math类基本
来源:互联网 发布:青少年性犯罪数据 编辑:程序博客网 时间:2024/03/29 14:32
Math类是一个专门进行数学计算的类,里面提供一系列的数学计算方法
Math类里面提供一切方法都是static型的方法,因为Math类没有普通属性
1.数学的四舍五入
格式: public static long round(double a)
例:
public class Nice { public static void main(String args[]) { System.out.println(Math.round(15.5)) ; //16 System.out.println(Math.round(-15.5)) ; //-15 System.out.println(Math.round(-15.51)) ; //-16 }}//负数四舍五入时,数据小数位大于0.5才进位,小于等于0.5不进位
2.随机数的产生
java.util.Random,可以产生随机数
例:
import java.util.Random;public class Nice { public static void main(String args[]) { Random rand = new Random() ; for(int x=0 ; x < 10 ; x++) { System.out.print(rand.nextInt(100) + "、"); } } }
//设计一个选号码程序,并且排好大小顺序import java.util.Random;public class Nice { public static void main(String args[]) { Random rand = new Random() ; int data[] = new int[10] ; //我要选十个数字 int foot = 0 ; while ( foot < 10) { //因为不知道次数,所以使用while int t = rand.nextInt(100) ; //100为上限 if(!isRepeat(data,t)) { //重复 data[foot++] = t ; } } java.util.Arrays.sort(data); for(int x=0 ; x < data.length ; x++) { System.out.print(data[x] + "、"); } } /* *此方法为判断是否有重复或者为0的内容 *存在的话 返回ture ,否则为false */ public static boolean isRepeat(int temp[], int num) { if(num == 0) { //没有必要判断 return true ; //返回flase,不再执行 } for(int x = 0 ; x < temp.length ; x++) { if(temp[x] == num) { return true ; } } return false ; }}
3.大数字的表示
大数字是什么?看下面例子:
public class Nice { public static void main(String args[]) { System.out.println(Double.MAX_VALUE * Double.MAX_VALUE); //Infinity }}//超出了double的范围
为了有超过double范围的数字可以计算,大数字的操作类就有 BigInteger、BigDecimal 两种
1)BigInteger
构造方法: public BigInteger(String val), 接收的是String型,是为了避免数字过大,无法表示
(BigInteger 基本的数学计算都可以操作)
import java.math.BigInteger ;public class Nice { public static void main(String args[]) { BigInteger bigA = new BigInteger("64646511348"); BigInteger bigB = new BigInteger("4321346654") ; System.out.println("加法操作:" + (bigA.add(bigB))) ; System.out.println("减法操作:" + (bigA.subtract(bigB))) ; System.out.println("乘法操作:" + (bigA.multiply(bigB))) ; System.out.println("加法操作:" + (bigA.divide(bigB))) ; //result数组里只有两个元素,一个是商,一个是余数 BigInteger result [] = bigA.divideAndRemainder(bigB) ; System.out.println("商:" + result[0] + ",余数:" + result[1]); }}
2)BigDecimal
BigInteger 不可以保存小数,BigDecimal可以保存小数
1.构造一: public BigDecimal(String val);
2.构造二: public BigDecimal(double val);
也可以进行基本基础数学计算,还可以用它来实现精准的四舍五入操作。
没有直接支持的四舍五入的方法,可以用除法实现:
除法操作: public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
1.BigDecimal divisor: 被除数
2.int scale: 保留的小数位
3.int roundingMode: 进位模式(public static final int ROUND_HALF_UP这个常量就是描述进位模式的,四舍五入的向上进位方式)
import java.math.BigDecimal ;class MyMath { //实现精准的四舍五入操作 public static double round(double num,int scale) { BigDecimal bigA = new BigDecimal(num) ; BigDecimal bigB = new BigDecimal(1) ; return bigA.divide(bigB,scale,BigDecimal.ROUND_HALF_UP).doubleValue() ; }}public class Nice { public static void main(String args[]) throws Exception{ System.out.println(MyMath.round(19.64646987,2)) ; //保留2位 System.out.println(MyMath.round(-传递传递15.5,0)) ; //现在就是-16.0了,正常的思维 }}
0 0
- Java学习提要——Math类基本
- Java学习提要——System类基本
- Java学习提要——File类的基本操作
- Java学习提要——String的基本方法
- Java学习提要——抽象类的使用限制
- Java学习提要——浅谈Object类
- Java学习提要——包装类与数据类型转换
- Java学习提要——StringBuffer类基础
- Java学习提要——Runtime类基础
- Java学习提要——日期处理类
- Java学习提要——字符串连接
- Java学习提要——方法引用
- Java学习提要——接口进阶
- Java学习提要——Callable接口
- Java学习提要——Comparable接口
- Java学习提要——输入流与输出流基本操作
- Java学习提要——字符流与字节流的基本操作与区别
- Java学习提要——基本网络编程Socket与echo
- HHUOJ 1013: 冒险游戏(risk_ganme) 动态规划 TWT tokyo olympic 2combo -2
- DirectX11:DirectX11下载和环境配置
- 【egret】代码结构
- JZOJ 4739 【雅礼联考GDOI2017模拟9.2】Ztxz16学图论
- 十六进制输入十进制输出的快速方法
- Java学习提要——Math类基本
- 计算程序的运行时间
- ucos学习I
- 乌龟棋
- 【算法导论-37】Graph的Java实现
- 奥利奥聊天机器人1.0【原创】【源码附注释】【C语言】
- 【PM】项目管理中遇到“额外”的事怎么处理
- 2-2 Time类的定义
- UNREAL ENGINE 4.13 正式发布!