猜數(shù)游戲
發(fā)布時(shí)間:2008-12-20 閱讀數(shù): 次 來(lái)源:網(wǎng)樂(lè)原科技
要完成此效果需要兩個(gè)步驟
第一步:把如下代碼加入到區(qū)域中
<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 = "哦,你猜的數(shù) "
if (num < myNumber) response = response + "太小了"
if (num > myNumber) response = response + "太大了"
if (num == myNumber)
{
response = response + "對(duì)了??!你猜到我了!"
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 = "我的數(shù)字是" + myNumber +". 想猜我的新數(shù)嗎?"
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>
我手中有一個(gè)數(shù),它的范圍在1~100之間,你能猜到它是幾嗎?
<script>
myNumber = random(100)+1
nGuesses = 0
</script>
第二步:把“onLoad="document.forms[0].guess.focus();
document.forms[0].guess.select()"”加在標(biāo)記里
例如: