JavaScript使用数学随机数来实现一个猜谜游戏。
请输入您的打卡号码
& ltbutton & gt竞争
& ltdiv & gt& lt/div & gt;
& lt脚本& gt
window.onload=function(){
document.querySelector("button ")。onclick=function(){
var?m = document . query selector(" div ");
var?x 1 = Number(document . query selector(" input ")。值);
if(x 1 & lt;1 | | x 1 & gt;3){
M.innerHTML= "您输入的数字有误,只能是1,2,3 ";
}否则{
var?x2 = math . floor(math . random()* 3)+1;
m . innerhtml = " Results:";
if(x1==x2){
m . innerhtml+= " even ";
}别的?if((x2-x 1)= = 1 | |(x 1-x2)= = 2){
M.innerHTML+= "你赢了";
}否则{
M.innerHTML+= "你输了";
}
m . innerhtml+= " & lt;br?/& gt;你打卡:"+x 1+" & lt;br?/& gt;电脑出拳:“+x2;
}
}
}
& lt/script & gt;