gpt4 book ai didi

javascript - 石头、布、剪刀、蜥蜴、史波克

转载 作者:行者123 更新时间:2023-12-03 08:17:51 26 4
gpt4 key购买 nike

我做错了什么?代码的前 2 部分有效,当它可以链接到 jpg 时图片就会显示,但我也希望屏幕显示谁获胜。

<html> <body>
<html>
<H1>Choose Wisely...</H1>
<img src="RockPaperScissorsLizardSpock2.jpg" id="bg" alt="">

<title>Rock, Paper, Scissors, Lizard, Spock</title>
</head>
<body>
<h2><p>Your Choice:</p>
<form action="form_action.asp">
<input type="radio" name="userchoice" id="Rock" onclick="myFunction(this.value)" >Rock<br>
<input type="radio" name="userchoice" id="Paper" onclick="myFunction(this.value)" >Paper<br>
<input type="radio" name="userchoice" id="Scissors" onclick="myFunction(this.value)" >Scissors<br>
<input type="radio" name="userchoice" id="Lizard" onclick="myFunction(this.value)" >Lizard <br>
<input type="radio" name="userchoice" id="Spock" onclick="myFunction(this.value)" >Spock<br><br>
</form>

<h2> Computer Choice: <p id="computer"></p>
<p id="compare"></p></h2>

<script>
function myFunction() {
var computer = Math.floor((Math.random() * 5)+1);
if (computer == 1) {computer = "Rock";
}
else if (computer == 2) {computer = "Paper";
}
else if (computer == 3) {computer = "Scissors";
}
else if (computer == 4) {computer = "Lizard";
}
else if (computer == 5) {computer = "Spock";
}
document.getElementById("computer").innerHTML = computer;}
</script>
<script>
var compare = function (choice1,choice2);
{
if (choice1 === choice2)
{
result "Draw, try again!";
}
else
{
if(choice1 === "Rock")
{
if(choice2 === "Paper")
{
result ("Paper covers Rock. You lose.");
}
else if(choice2 === "Scissors")
{
result ("Rock crushes Scissors. You win.");
}
else if(choice2 === "Lizard")
{
result ("Rock smashes Lizard. You win.");
}
else if(choice2 === "Spock")
{
result ("Spock vaporizes Rock. You lose.");
}
}
}
else
{
if(choice1 === "Paper")
{
if(choice2 === "Rock")
{
return "Paper covers Rock. You Win.";
}
else if(choice2 === "Scissors")
{
return "Scissors cut Paper. You lose.";
}
else if(choice2 === "Lizard")
{
return "Lizard eats Paper. You lose.";
}
else if(choice2 === "Spock")
{
return "Paper disproves Spock. You Win.";
}
}
}
else
{
if(choice1 === "Scissors")
{
if(choice2 === "Rock")
{
return "Rock beats Scissors. You lose.";
}
if(choice2 === "Paper")
{
return "Scissors cut Paper. You win.";
}
else if(choice2 === "Lizard")
{
return "Scissors decapitate Lizard. You win.";
}
else if(choice2 === "Spock")
{
return "Spock smashes Scissors. You lose.";
}
}
}
else
{
if(choice1 === "Lizard")
{
if(choice2 === "Rock")
{
return "Rock smashes Lizard. You lose.";
}
if(choice2 === "Paper")
{
return "Lizard eats Paper. You win.";
}
else if(choice2 === "Scissors")
{
return "Scissors decapitate Lizard. You lose.";
}
else if(choice2 === "Spock")
{
return "Lizard poisons Spock. You win.";
}
}
}
else
{
if(choice1 === "Spock")
{
if(choice2 === "Rock")
{
return "Spock vaporizes Rock. You win.";
}
if(choice2 === "Paper")
{
return "Paper disproves Spock. You lose.";
}
else if(choice2 === "Lizard")
{
return "Lizard poisons Spock. You lose.";
}
else if(choice2 === "Scissors")
{
return "Spock smashes Scissors. You win.";
}
}
}
};
compare(id,computer);

我已经尝试过console.log(compare(id,computer))document.getElementById("compare").innerHTML = compare;}都不起作用

还尝试将 id 更改为整数并添加

function myFunction(myText) {
if computer == 1 and id == 1{
document.getElementById("myText").value = "Rock vs Rock, Stalemate!"}
else if computer == 1 and id == 2{
document.getElementById("myText").value = "Paper Covers Rock,Loser!"}
else if computer == 1 and id == 3{
document.getElementById("myText").value = "Rock Crushes Scissors, Winner!"}
else if computer == 1 and id == 4{
document.getElementById("myText").value = "Rock Crushes Lizard, Winner!"}
else if computer == 1 and id == 5{
document.getElementById("myText").value = "Spock Vaporizes Rock, Loser!"}
}
</script>

最佳答案

Plunkr Demo

大多数信息都添加在代码下面的注释中。

您的代码存在多个问题,主要是您处理 else if 的方式。请注意,下面的代码写得仍然很差,可以优化。

您还调用了 compare(id, computer)id 未定义。此外,您还传递了单选按钮的 this.value onclick,但这不起作用,因为所有这些按钮的值都是 on,这会更改为 this.id 下面,因为它将传递预期值。

HTML

<!DOCTYPE html>
<html>

<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<title>Rock, Paper, Scissors, Lizard, Spock</title>
</head>

<body>
<!-- moved from head to body -->
<H1>Choose Wisely...</H1>
<!-- moved from head to body -->
<img src="RockPaperScissorsLizardSpock2.jpg" id="bg" alt="">
<!-- changed <p> to <div> added </h2> to the end -->
<h2><div>Your Choice:</div></h2>
<form action="form_action.asp">
<input type="radio" name="userchoice" id="Rock" value="1" onclick="myFunction(this.id)">Rock
<br>
<input type="radio" name="userchoice" id="Paper" value="2" onclick="myFunction(this.id)">Paper
<br>
<input type="radio" name="userchoice" id="Scissors" value="3" onclick="myFunction(this.id)">Scissors
<br>
<input type="radio" name="userchoice" id="Lizard" value="4" onclick="myFunction(this.id)">Lizard
<br>
<input type="radio" name="userchoice" id="Spock" value="5" onclick="myFunction(this.id)">Spock
<br>
<br>
</form>

<!-- changed <p> to <div> -->
<h2>Computer Choice: <div id="computer"></div>
<div id="compare"></div></h2>
</body>

</html>

JS

// Code goes here

function myFunction(value) {
var computer = Math.floor((Math.random() * 5) + 1);
if (computer == 1) {
computer = "Rock";
} else if (computer == 2) {
computer = "Paper";
} else if (computer == 3) {
computer = "Scissors";
} else if (computer == 4) {
computer = "Lizard";
} else if (computer == 5) {
computer = "Spock";
}

document.getElementById("computer").innerHTML = computer;
//added compare call and updating the innerHTML of the compare id DOM object
document.getElementById("compare").innerHTML = compare(value, computer);
}

var compare = function(choice1, choice2) {
if (choice1 === choice2) {
// changed to return
return "Draw, try again!";
}
//changed to else if instead of else {
else if (choice1 === "Rock") {
if (choice2 === "Paper") {
return "Paper covers Rock. You lose.";
} else if (choice2 === "Scissors") {
return "Rock crushes Scissors. You win.";
} else if (choice2 === "Lizard") {
return "Rock smashes Lizard. You win.";
} else if (choice2 === "Spock") {
return "Spock vaporizes Rock. You lose.";
}
}
//changed to else if instead of else {
else if (choice1 === "Paper") {
if (choice2 === "Rock") {
return "Paper covers Rock. You Win.";
} else if (choice2 === "Scissors") {
return "Scissors cut Paper. You lose.";
} else if (choice2 === "Lizard") {
return "Lizard eats Paper. You lose.";
} else if (choice2 === "Spock") {
return "Paper disproves Spock. You Win.";
}
}
//changed to else if instead of else {
else if (choice1 === "Scissors") {
if (choice2 === "Rock") {
return "Rock beats Scissors. You lose.";
}
//changed to else if instead of if {
else if (choice2 === "Paper") {
return "Scissors cut Paper. You win.";
} else if (choice2 === "Lizard") {
return "Scissors decapitate Lizard. You win.";
} else if (choice2 === "Spock") {
return "Spock smashes Scissors. You lose.";
}
}
//changed to else if instead of else {
else if (choice1 === "Lizard") {
if (choice2 === "Rock") {
return "Rock smashes Lizard. You lose.";
}
//changed to else if instead of if {
else if (choice2 === "Paper") {
return "Lizard eats Paper. You win.";
} else if (choice2 === "Scissors") {
return "Scissors decapitate Lizard. You lose.";
} else if (choice2 === "Spock") {
return "Lizard poisons Spock. You win.";
}
}
//changed to else if instead of else {
else if (choice1 === "Spock") {
if (choice2 === "Rock") {
return "Spock vaporizes Rock. You win.";
}
//changed to else if instead of if {
else if (choice2 === "Paper") {
return "Paper disproves Spock. You lose.";
} else if (choice2 === "Lizard") {
return "Lizard poisons Spock. You lose.";
} else if (choice2 === "Scissors") {
return "Spock smashes Scissors. You win.";
}
}
}

关于javascript - 石头、布、剪刀、蜥蜴、史波克,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33862412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com