zoukankan      html  css  js  c++  java
  • 1:Math数学函数

    知识梳理

    //1 Math.floor();向上取整
    //2 Math.ceil();向下取整
    //3 Math.round();四舍五入取整
    //4 Math.random(); 返回一个大于等于0小于1的浮点数

    Math.round(-1.5); 返回是-1 而不是-2
    因为是向上取整 -1比-2大

    一  基本使用

    // Math是一个内置对象 不是构造函数 直接使用就可以

    二 返回随机数

    1)基本用法

    //返回一个随机小数 x >= 0   x < 1;
    console.log(Math.random());//没有参数

    2)得到两数之间的一个随机整数

    //得到一个两数之间的随机整数,包括两个数在内
    //公式 Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
    
    function getRandom(max,min) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    console.log(getRandom(10,100)); //需要自己封装函数

    3)随机点名

    function getRandom(max,min) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    var arr = ['小王','小栗','小花','小牛','小黄'];
    console.log(arr[getRandom(0,arr.length -1)]);

    5)猜字谜

    //获取随机数函数
    function getRandom(max,min) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    
    var num = getRandom(1,10);//得到一个随机数
    
    while (true) { //死循环 [ 后面必须有结束循环的 break ]
        var value = prompt('数字谜你来猜 1-10之间');
        if (value > num) {
            alert('猜大了');
        } else if (value < num) {
            alert('猜小了')
        } else {
            alert('猜对了');
            break; //退出整个循环 程序结束
        }
    }

  • 相关阅读:
    [MacOS]修改Wifi默认共享网段
    [CentOS7]升级OpenSSL至1.1.1
    [Linux]checking for libevent support... configure: error: Unable to use libevent (libevent check failed)
    [CentOS7]team模式切换
    HDU 5416 CBR and tree
    CodeForces 374D Inna and Sequence
    HDU 5981 Guess the number
    题目清单
    HDU 5510 Bazinga
    KMP & AC自动机
  • 原文地址:https://www.cnblogs.com/fuyunlin/p/14407558.html
Copyright © 2011-2022 走看看