Java学习笔记-Math类

并非所有的类都需要main方法.Math类和JOptionPane类都没有main方法.这些类中所包含的方法主要是为了供其他类使用.

package welcome;

public class TestMath {
    public static void main(String[] args) {
        // 三角函数方法
        System.out.println(Math.toDegrees(Math.PI / 2));
        System.out.println(Math.toRadians(30));
        System.out.println(Math.sin(0));
        System.out.println(Math.toRadians(270));
        System.out.println(Math.sin(Math.PI / 6));
        System.out.println(Math.sin(Math.PI / 2));
        System.out.println(Math.sin(Math.PI / 3));
        System.out.println(Math.cos(0));
        System.out.println(Math.cos(Math.PI / 6));
        System.out.println(Math.cos(Math.PI / 2));
        System.out.println(Math.asin(0.5));
        
        System.out.println("-----------------------");
        // 指数函数方法
        System.out.println(Math.exp(1));
        System.out.println(Math.log(Math.E));
        System.out.println(Math.log10(10));
        System.out.println(Math.pow(2, 3));
        System.out.println(Math.pow(3, 2));
        System.out.println(Math.pow(3.5, 2.5));
        System.out.println(Math.sqrt(4));
        System.out.println(Math.sqrt(10.5));
        
        System.out.println("-------------------------");
        // 取整方法
        System.out.println(Math.ceil(2.1));
        System.out.println(Math.ceil(2.0));
        System.out.println(Math.ceil(-2.0));
        System.out.println(Math.ceil(-2.1));
        System.out.println(Math.floor(2.1));
        System.out.println(Math.floor(2.0));
        System.out.println(Math.floor(-2.0));
        System.out.println(Math.floor(-2.1));
        System.out.println(Math.rint(2.1));
        System.out.println(Math.rint(-2.0));
        System.out.println(Math.rint(-2.1));
        System.out.println(Math.rint(2.5));
        System.out.println(Math.rint(3.5));
        System.out.println(Math.rint(-2.5));
        System.out.println();
        System.out.println(Math.round(2.6F));
        System.out.println(Math.round(2.0));
        System.out.println(Math.round(-2.6));
        System.out.println(Math.round(-2.0F));
        
        // min, max 和 abs方法
        
        System.out.println("min, max 和 abs方法");
        System.out.println(Math.min(2, 3));
        System.out.println(Math.max(2.5, 3));
        System.out.println(Math.max(2.5, 3.6));
        System.out.println(Math.abs(-2));
        System.out.println(Math.abs(-2.1));
        
        // random方法
        System.out.println((int)(Math.random() * 100)); // 返回0到99之间的一个随机整数
        System.out.println(50 + (int)(Math.random() * 50)); // 返回50到99之间的一个随机整数
        
        // a + Math.random() * b 返回a到a+b之间但不包括a+b的一个随机数
    }
}

 

转载于:https://www.cnblogs.com/datapool/p/6258923.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值