zoukankan      html  css  js  c++  java
  • java基础23 Math类和Random类

    一、Math数学类

       主要是提供很多数学的公式

    1.1、Math类的常用方法

       abs(int a):绝对值
       ceil(double a):向上取整
       floor(double a):向下取整
       round(float a):四舍五入
       random():大于等于 0.0且小于 1.0的伪随机 double值

    1.2、实例

     1 package com.dhb.code;
     2 
     3 import java.util.Random;
     4 
     5 /**
     6  * @author DSHORE / 2018-5-2
     7  *
     8  */
     9 public class Demo8 {
    10     public static void main(String[] args) {
    11         System.out.println("绝对值:"+Math.abs(-3));//返回值:3
    12         System.out.println("绝对值:"+Math.abs(-3.0));//返回值:3.0
    13         
    14         System.out.println("向上取整:"+Math.ceil(3.4));//返回值:4.0
    15         System.out.println("向上取整:"+Math.ceil(3.6));//返回值:4.0
    16         System.out.println("向上取整:"+Math.ceil(-3.4));//返回值:-3.0
    17         System.out.println("向上取整:"+Math.ceil(-3.6));//返回值:-3.0
    18         
    19         System.out.println("向下取整:"+Math.floor(3.3));//返回值:3.0
    20         System.out.println("向下取整:"+Math.floor(3.6));//返回值:3.0
    21         System.out.println("向下取整:"+Math.floor(-3.3));//返回值:-4.0
    22         System.out.println("向下取整:"+Math.floor(-3.6));//返回值:-4.0
    23         
    24         System.out.println("四舍五入:"+Math.round(3.4));//返回值:3
    25         System.out.println("四舍五入:"+Math.round(3.5));//返回值:4
    26         System.out.println("四舍五入:"+Math.round(3.6));//返回值:4
    27         System.out.println("四舍五入:"+Math.round(-3.4));//返回值:-3
    28         System.out.println("四舍五入:"+Math.round(-3.5));//返回值:-3    注意:3.5和-3.5的区别
    29         System.out.println("四舍五入:"+Math.round(-3.6));//返回值:-4
    30         
    31         System.out.println("随机数:"+Math.random());//任意产生一个在0.0<=随机数<1.0之间的随机数。
    32         
    33         Random random = new Random();
    34         int i = random.nextInt(10);//nextInt():返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值
    35         System.out.println(i);//这里产生的数为"0<=i<10"的整数
    36     }
    37 }

    二、Random类

    package com.dhb.code;
    
    import java.util.Random;
    
    /*
     * 随机数:
     * Random
     * 
     * 需求:编写一个函数随机产生四位验证码.
     * */
    public class Demo2 {
        public static void main(String[] args) {
            Random r=new Random();
            int t=r.nextInt(10)+1;//加1:表示随机产生的整数是:1<=t<11 即:[1,10]或[1,11)。结果是整数,只能是1,2,3,4,5,6,7,8,9,10 其中之一
            System.out.println("随机数:"+t);
    
            char[] arr={'','','','c','a','q','s','z'};
            StringBuilder sb=new StringBuilder();
            Random r=new Random();
            //需要四个随机数,通过随机数获取字符数组中的字符
            for(int i=0;i<4;i++){
                int index=r.nextInt(arr.length);
                sb.append(arr[index]);
            }
            System.out.println("验证码:"+sb);
        }
    }

    原创作者:DSHORE

    作者主页:http://www.cnblogs.com/dshore123/

    原文出自:http://www.cnblogs.com/dshore123/p/8980439.html

    欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

  • 相关阅读:
    20200209 ZooKeeper 3. Zookeeper内部原理
    20200209 ZooKeeper 2. Zookeeper本地模式安装
    20200209 Zookeeper 1. Zookeeper入门
    20200206 尚硅谷Docker【归档】
    20200206 Docker 8. 本地镜像发布到阿里云
    20200206 Docker 7. Docker常用安装
    20200206 Docker 6. DockerFile解析
    20200206 Docker 5. Docker容器数据卷
    20200206 Docker 4. Docker 镜像
    Combining STDP and Reward-Modulated STDP in Deep Convolutional Spiking Neural Networks for Digit Recognition
  • 原文地址:https://www.cnblogs.com/dshore123/p/8980439.html
Copyright © 2011-2022 走看看