<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Guess the number game</title> <style type="text/css"> #my_guess_num { height: 18px; vertical-align: middle; text-align: center; line-height: 18px; /*line-height must be equal to height*/ } </style> </head> <script> var count = 1; var num = null; //get random number function GetRandomNum(Min, Max) { count = 1; var Range = Max - Min; var Rand = Math.random(); num = Min + Math.round(Rand * Range); alert("The random number has been generated, please start guessing!" + num) } //get the textbox content function checknum () { if (num == null) { alert("Please generate a random number before you start guessing!"); return; } var nums = document.getElementById("my_guess_num").value; if ("" == nums) { alert("Please enter the number you want to guess!"); return; } if (nums == num) { alert("Congratulations on trying " + count + " times!"); return; } else { alert("Number of guesses" + count) if (count == 10) { if (nums != num) { alert("Guess wrong, you have no chance, what a pity! Regenerate the random number again!") return; } } if (nums < num) { alert("The number is a little small, you still have " + (10 - count) + "chance"); } else { alert("The number is too big, you still have " + (10 - count) + "chance"); } count++; } } </script> <body> <h1>Number Guessing Game</h1> Description: Click [Generate Random Number] to generate a random number from 1 to 99, then enter the number you want to guess, and click [Guess]; there are 10 chances in total; <br /> <br /> <input type="button" id="result" name="result" onclick="GetRandomNum(1,100)" value="Generate random number" /> <br /> <br /> <input type="text" id="my_guess_num" name="my_guess_num" /> <input type="button" id="guess" name="guess" onclick="checknum()" value="猜" /> </body> </html>
Number guessing game on page (html/jsp)
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325437944&siteId=291194637
Recommended
Ranking