PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

javascript怎么求三个数的最大值

藏色散人
藏色散人 原创
2021-07-01 11:32:27 7164浏览

javascript求三个数的最大值的方法:首先创建一个html示例文件;然后输入三个值;最后通过“math.max(inps[0].value,inps[1].value,inps[2].value)”求三个数的最大值即可。

本文操作环境:windows7系统、javascript1.8.5版,DELL G3电脑

javascript怎么求三个数的最大值?

javascript中输入三个数求最大值方法

bc02d5d01c176605957d5c972ad9a02.png

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input type="text"><br>
<input type="text"><br>
<input type="text"><br>
<input type="button" value="确认">
</body>
<script>
var inps = document.querySelectorAll("input");
inps[3].onclick = function(){
alert(Math.max(inps[0].value,inps[1].value,inps[2].value));
      //得到的.value是string型的,需要转换为number
        var a = Number(inps[0].value);
        var b = Number(inps[1].value);
        var c = Number(inps[2].value);
 
        if(a>b){
          if(a>c){
           alert(a);
          }else{
           alert(c);
          }
        }else{
         if(b>c){
         alert(b);
         }else{
         alert(c);
         }
        }
 
 
       // 枚举法 列举所有可能
       if(a>=b && a>=c){
        alert(a);
       }
       if(b>=a && b>=c){
        alert(b);
       }
       if(c>=a && c>=b){
        alert(c);
       }
 
 
 
       //定义中间变量,定义最大值
       var max = 0;
       if(a >= max){
        max = a;
       }
       if(b >= max){
        max = b;
       }
       if(c >= max){
        max = c;
       }
       alert(max);
 
 
}
</script>
</html>

推荐学习:《javascript高级教程

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。