我做了一个石头、纸、剪刀和代码末尾的按钮的游戏,单击一次后,它就消失了,一次又一次地重复该代码是没有意义的。
function game(){
var userChoice= prompt("Do you choose rock, paper, or scissors?");
document.write("You chose " + userChoice + ". <br/>")
var computerChoice= Math.random();
if(computerChoice<=0.33){
computerChoice="rock";
}
else if(computerChoice<=0.67){
computerChoice="paper";
}
else{
computerChoice="scissors";
}
document.write("The computer chose " + computerChoice + ". <br/>");
if(userChoice==computerChoice){
document.write("TIE!!!!");
}
else if(userChoice=="paper" && computerChoice=="rock"){
document.write('<span style="color:red">You win!</span>');
}
else if(userChoice=="rock" && computerChoice=="scissors"){
document.write('<span style="color:red">You win!</span>');
}
else if(userChoice=="scissors" && computerChoice=="paper"){
document.write('<span style="color:red">You win!</span>');
}
else if(userChoice=="scissors" && computerChoice=="rock"){
document.write('Sorry, you lose.');
}
else if(userChoice=="rock" && computerChoice=="paper"){
document.write("Sorry, you lose.");
}
else if(userChoice=="paper" && computerChoice=="scissors"){
document.write("Sorry, you lose.");
}
else if(userChoice=="Chuck Norris"){
document.write("This program bows down to your superiority, YOU WIN!!!!");
}
document.write('<button type="button" style="display:block;" onclick="game()">Play Again!</button>');
}
<body onload="game()">