练习9_获胜率游戏.html 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. var count = 0;
  12. function playGame(){
  13. // 获取1-9随机数字
  14. var num1 = Math.floor(Math.random()*9+1);
  15. var num2 = Math.floor(Math.random()*9+1);
  16. var num3 = Math.floor(Math.random()*9+1);
  17. console.log(num1,num2,num3);
  18. // 判断游戏是否获胜
  19. if(num1==8 || num2==8 || num3==8){
  20. count++;
  21. console.log("游戏获胜");
  22. }else{
  23. console.log("游戏失败");
  24. }
  25. }
  26. // playGame();
  27. // 运行10次游戏
  28. for(var i=0;i<10;i++){
  29. playGame()
  30. }
  31. // 计算获胜率
  32. var winRate = count/10;
  33. console.log("获胜率为:"+winRate*100+"%");
  34. </script>
  35. </body>
  36. </html>