js求三個數的最大值運算


js代碼:
<script>
    // var num1 = 32,
    //   num2 = 43,
    //   num3 = 98;

    // if (num1 > num2) {
    //   if (num1 > num3) {
    //     alert(num1);
    //   } else {
    //     alert(num3);
    //   }
    // } else {
    //   if (num2 > num3) {
    //     alert(num2);
    //   } else {
    //     alert(num3);
    //   }
    // }



    var num1 = Number(prompt('請輸入數字'));
    var num2 = Number(prompt('請輸入數字'));
    var num3 = Number(prompt('請輸入數字'));
    //邏輯與(邏輯且)求三個數中的最大值
    // if (num1 > num2 && num1 > num3) {
    //   alert(num1);
    // } else if (num2 > num1 && num2 > num3) {
    //   alert(num2);
    // } else {
    //   alert(num3);
    // }



    //三元求三個數中的最大值
    var max = (num1 > num2 ? num1 : num2) > num3 ? (num1 > num2 ? num1 : num2) : num3;
    alert(max);
  </script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM