document.write("<input type='button' onclick='Guess()' value='提交'>")//按钮
function Guess() {
var inputVal=document.getElementById("inval").value //获取文本框的值
var curRand=parseInt(100*Math.random()) //获取随机数
if(!isNum(inputVal))
{
alert("不是数字,请重新输入")
return
}
if(inputVal){
if(inputVal>curRand){
alert("胜利")
} else if (inputVal<curRand){
alert("输了")
} else {
alert("平局")
}
}
}
//判断是否是数字,true表示是数字
function isNum(value) {
return !isNaN(value)
}
<HTML><HEAD>
<SCRIPT LANGUAGE="JavaScript">
var js_mult1=3141
var js_mult2=5821
var js_m1=100000000
var js_m2=10000
var js_iseed=0
var js_iseed1=0
var js_iseed2=0
function random(n)
{
if (js_iseed == 0)
{
now = new Date()
js_iseed = now.getHours() + now.getMinutes() * 60
+ now.getSeconds() * 3600
}
js_iseed1 = js_iseed / js_m2
js_iseed2 = js_iseed % js_m2
var tmp = (((js_iseed2 * js_mult1 + js_iseed1 * js_mult2) % js_m2) *
js_m2 + (js_iseed2 * js_mult2)) % js_m1
js_iseed = (tmp + 1) % js_m1
return (Math.floor((js_iseed/js_m1) * n))
}
var myNumber = 0
var nGuesses = 0
function guessnum()
{
var response
var num = parseInt(document.forms[0].guess.value)
document.forms[0].guess.value = num // visual feedback in case of NaN
nGuesses++ // increase the guess count
response = "你猜的数字"
if (num <myNumber)response = response + "小了"
if (num >myNumber)response = response + "大了"
if (num == myNumber)
{
response = response + "猜对了!! 继续下一个数字吧."
nGuesses = 0
myNumber = random(100)+1
}
document.forms[1].result.value=response
document.forms[1].guesses.value=nGuesses
document.forms[0].guess.focus()
document.forms[0].guess.select()
return true
}
function giveup()
{
var response
nGuesses = 0
response = "这个数字是 " + myNumber +"。别灰心,猜下一个数字吧"
myNumber = random(100)+1
document.forms[1].result.value=response
document.forms[1].guesses.value=nGuesses
document.forms[0].guess.focus()
document.forms[0].guess.select()
}
</SCRIPT></HEAD>
<BODY bgcolor="ffffff" onLoad="document.forms[0].guess.focus()document.forms[0].guess.select()">
<center>
<H2>猜数字游戏</H2><p><font size="2"><b>数字是1-100内的数字,随机显示,猜猜看</b></font><p>
<SCRIPT>
myNumber = random(100)+1
nGuesses = 0
</SCRIPT>
<FORM onSubmit="guessnum()return false">
你猜的数字是: <INPUT TYPE="text" NAME="guess" SIZE=5>
<INPUT TYPE="button" VALUE=" 猜猜... " onClick="guessnum()">
<INPUT TYPE="button" VALUE=" 放弃 " onClick="giveup()">
</FORM>
<FORM><PRE>
次数: <INPUT TYPE="text" NAME="guesses" SIZE=3>
信息提示: <INPUT TYPE="text" NAME="result" SIZE=44>
</PRE></FORM>