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

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

  • 相关阅读:
    还是火柴排队(补一下归并排序的锅)
    火柴排队(NOIP2013)(附树状数组专题讲解(其实只是粗略。。。))
    转圈游戏(NOIP2013)
    接口和多态
    HttpClient-传入url得到json字符串( PostMethod method = new PostMethod(url)是个好方法)
    url和资源的再理解
    每天进步一点点- 资源与URI(吐血精华总结)
    每天进步一点点-一切皆对象/一次编写,到处运行/bean工厂
    java黑魔法-反射机制-02-通过Java反射调用其他类方法
    java黑魔法-反射机制-01
  • 原文地址:https://www.cnblogs.com/dshore123/p/8980439.html
Copyright © 2011-2022 走看看