zoukankan      html  css  js  c++  java
  • 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的一个随机数
        }
    }
  • 相关阅读:
    2015/11/2用Python写游戏,pygame入门(2):游戏中的事件和显示
    2015/11/1用Python写游戏,pygame入门(1):pygame的安装
    2015/10/13 算法习题:最大子列和问题
    2015/10/9 Python核编初级部分学习总结
    2015/10/9 Python基础(21):可调用和可执行对象
    2015/9/29 Python基础(20):类的授权
    2015/9/28 Python基础(19):类的定制和私有性
    2015/9/22 Python基础(18):组合、派生和继承
    2015/9/21 Python基础(17):绑定和方法调用
    MVC 依赖注入
  • 原文地址:https://www.cnblogs.com/datapool/p/6258923.html
Copyright © 2011-2022 走看看