• 21.1 Math(数学运算)方法使用 、工具类


    package day21_static.meathDemo;
    //Math: 包含一些基本的数学运算方法
    //从api中搜Math,它都用的static修饰。
    public class MethDemo {
        public static void main(String[] args) {
            //打印圆周率
            System.out.println(Math.PI);
    
            //static double abs(double a)  :返回绝对值
            System.out.println(Math.abs(10));
            System.out.println(Math.abs(-12));
    
            System.out.println(Math.ceil(1.1));
            System.out.println(Math.ceil(1.7)); //向上取整
            System.out.println(Math.floor(1.6)); //向下取整
            System.out.println(Math.round(1.5)); //四舍五入
    
            System.out.println(Math.max(4,6));
    
            //static double pow(double a, double b) :返回第一个参数的第二个参数次幂
            System.out.println(Math.pow(3,2));
    
            //static double random() :返回一个随机数,大于零且小于一
            System.out.println(Math.random());
        }
    }

    输出如下

    package day21_static.gongju;
    
    public class MyArrays {
        //这是一个和数组操作有关的工具类,不需要创建对象,所以我们可以私有他的构造方法
        private MyArrays() {}
    
        //获取最大值
        public static int getMax(int[] arr) {
            int max = arr[0];
            for(int i=1;i<arr.length;i++) {
                if(arr[i] >  max) {
                    max = arr[i];
                }
            }
            return max;
        }
    
        //获取指定数值的索引
    
        public static int getIndex(int[] arr,int a) {
            for(int i=0;i<arr.length;i++) {
                if(a == arr[i]) {
                    return i;
                }
            }
    
            return -1;
        }
    }
    package day21_static.gongju;
    
    
    public class MyArraysUse {
        public static void main(String[] args) {
            int[] arr = {3,2,5,9,7};
    
            int index = MyArrays.getIndex(arr,2);
            int max1 = MyArrays.getMax(arr);
    
            System.out.println(index);
            System.out.println(max1);
        }
    }

    输出如下

  • 相关阅读:
    Burnside引理与Polya定理 学习笔记
    Codeforces 438E. The Child and Binary Tree 多项式,FFT
    Berlekamp_Massey 算法 (BM算法) 学习笔记
    UOJ#335. 【清华集训2017】生成树计数 多项式,FFT,下降幂,分治
    UOJ#73. 【WC2015】未来程序 提交答案题
    UOJ#206. 【APIO2016】Gap 构造 交互题
    虚拟机配置JAVA_HOME
    创建虚拟机
    月份、季度、周
    maven多模块下使用JUnit进行单元测试
  • 原文地址:https://www.cnblogs.com/longesang/p/11190116.html
Copyright © 2020-2023  润新知