​在编写程序时,可能需要计算一个数的平方根、绝对值或获取一个随机数等。java.lang包中的Math类包含许多用来进行科学计算的static方法,这些方法可以直接通过类名调用。另外,Math类还有两个static常量:E和PI。


以下仅列举一些常用方法

import java.math.*;

public class Test {
public static void main(String[] args){
//返回a的绝对值
double a = -5.0;
double abs = Math.abs(a);
System.out.println("a的绝对值:"+abs); //返回两个数中的最大值、最小值
double b =10.0;
double max = Math.max(a,b);
double min = Math.min(a,b);
System.out.println("a,b的最大值为:"+max);
System.out.println("a,b的最小值为:"+min); //产生一个0-1之间的随机数(包括0,不包括1)
double random = Math.random();
System.out.println("产生一个0-1之间的随机数:"+random); //返回a的3次幂
double pow = Math.pow(a,3);
System.out.println("a的3次幂为:"+pow); //返回b的平方根
double sqrt = Math.sqrt(b);
System.out.println("b的平方根为:"+sqrt); //返回c的对数
double c = 8.0;
double log = Math.log(c);
System.out.println("c的对数为:"+log); //返回d的正弦值
double d = 0.5;
double sin = Math.sin(d);
System.out.println("d的正弦值为:"+sin); //返回d的反正弦值
double asin = Math.asin(d);
System.out.println("d的反正弦值为:"+asin); //返回大于d的最小整数,并将该整数转化为double数据
double ceil = Math.ceil(d);
System.out.println("大于d的最小整数为:"+ceil); //返回小于d的最大整数,并将该整数转化为double数据
double floor = Math.floor(d);
System.out.println("小于d的最大整数为:"+floor); //返回某个数的四舍五入的值
System.out.println(Math.round(15.6));
System.out.println(Math.round(15.4));
System.out.println(Math.round(-15.5));
System.out.println(Math.round(-15.6));
/*
如果该数为非负数,小数大于或等于0.5入,小于0.5舍
如果该数为负数,小数大于0.5入,小于或等于0.5舍
*/
}
}

Java中Math类常用方法的更多相关文章

  1. java基础-Math类常用方法介绍

    java基础-Math类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Math类概念 Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函 ...

  2. Java中math类的常用函数

    Java中math类的常用函数 在 Java 中 Math 类封装了常用的数学运算,提供了基本的数学操作,如指数.对数.平方根和三角函数等 只要在源文件的顶部加上下面这行代码就不必在数学方法名和常量名 ...

  3. JAVA中String类常用方法 I

    String类常用方法有: int length() -– 返回当前字符串的长度 int indexOf(int ch) -– 查找ch字符在该字符串中第一次出现的位置 int indexOf(Str ...

  4. java 中 Math类

    package cn.liuliu.com; import java.math.BigDecimal; import java.math.BigInteger; /* * 一.Math类? * * 1 ...

  5. java中Math类

    Math类 Math类是一个很有用的数学帮助类,使用也非常简单,这个类比较特殊,首先他和String类一样都是用final修饰,所以不能有子类,还有就是它的构造方法是私有的,也就是我们不能通过new的 ...

  6. Java中Math类的常用方法

    public class MathDemo { public static void main(String args[]){ /** * abs求绝对值 */ System.out.println( ...

  7. JAVA中StringBuffer类常用方法详解

    String是不变类,用String修改字符串会新建一个String对象,如果频繁的修改,将会产生很多的String对象,开销很大.因此java提供了一个StringBuffer类,这个类在修改字符串 ...

  8. JAVA中StringBuffer类常用方法

    String是不变类,用String修改字符串会新建一个String对象,如果频繁的修改,将会产生很多的String对象,开销很大.因此java提供了一个StringBuffer类,这个类在修改字符串 ...

  9. Java中 ArrayList类常用方法和遍历

     ArrayList类对于元素的操作,基本体现在——增.删.查.常用的方法有: public boolean add(E e) :将指定的元素添加到此集合的尾部. public E remove(in ...

  10. Java中String类常用方法(字符串中的子字符串的个数)

    重点内容 4种方法: 1.int indexOf(String str)返回第一次出现的指定子字符串在此字符串中的索引. 2.int indexOf(String str, int startInde ...

随机推荐

  1. docker-compose任务编排

    一.docker-compose介绍 使用一个Dockerfile模板文件可以定义一个单独的应用容器,如果需要定义多个容器就需要服务编排.下面介绍Docker官方产品,Docker Compose. ...

  2. iOS Programing

    ARC 1. ARC - Automatic Reference Counting 办公室照明原理 2. 函数release立即释放,autorelease进入autoreleasepool里 3. ...

  3. Python系统模块os.py和sys.py常用函数

    OS模块 os模块就是对操作系统进行操作,使用该模块必须先导入模块: import os #getcwd() 获取当前工作目录(当前工作目录默认都是当前文件所在的文件夹) result = os.ge ...

  4. 掌控安全学院SQL注入靶场延时注入(二)

    单引号被转义了 尝试闭合 判断注入 利用盲注即可 or ascii(substr(database(),1,1))=119

  5. 可收集ALC问题[Microsoft.Data.SqlClient is not supported on this platform.]

    异常 Microsoft.Data.SqlClient is not supported on this platform. 堆栈跟踪 at Microsoft.Data.SqlClient.SqlC ...

  6. 【rocky linux】firewalld切换到iptables 以及Failed to start IPv4 firewall with iptables.

    关闭[firewalld],安装iptables并启动 #关闭自带 firewall systemctl stop firewalld systemctl mask firewalld#显示当前 fi ...

  7. linux dma

    https://bootlin.com/pub/conferences/2015/elc/ripard-dmaengine/ripard-dmaengine.pdf https://biscuitos ...

  8. paddle 错误(ValueError: all input arrays must have the same shape)

    参考:voc数据集执行eval.py命令报错 · Issue #3456 · PaddlePaddle/PaddleDetection (github.com) 配置文件加这两行: EvalReade ...

  9. 力扣1143. 最长公共子序列(dp)

    给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度.如果不存在 公共子序列 ,返回 0 . 一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符 ...

  10. Antlr语法优化过程记录

    背景 Modelica Spec中的语法文件在Antlr下表现很糟糕,至少是1个数量级的糟糕的性能表现 理论 语义谓词减慢速度 ATN图中多分支转换为单分支 可选放在词法开头和语法的结尾 避免前导可选 ...