练习题19_获胜率.html 958 B

123456789101112131415161718192021222324252627282930313233343536
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 计算获胜率
  11. function WinRate(n){
  12. // 生成1-9之间的随机数
  13. var num1 = Math.floor(Math.random()*9)+1;
  14. var num2 = Math.floor(Math.random()*9)+1;
  15. var num3 = Math.floor(Math.random()*9)+1;
  16. console.log(num1,num2,num3);
  17. if(num1 == 8 || num2 == 8 || num3 == 8) {
  18. return true;
  19. }else{
  20. return false;
  21. }
  22. }
  23. var winCount = 0;
  24. var playCount = 10;
  25. for(var i = 0;i<playCount;i++){
  26. // 每次游戏都调用WinRate函数
  27. if(WinRate()){
  28. winCount++;
  29. }
  30. }
  31. console.log((winCount/playCount)*100 + "%");
  32. </script>
  33. </body>
  34. </html>