gpt4 book ai didi

javascript - 在 JavaScript 中使用石头剪刀布蜥蜴 Spock Final 时遇到问题

转载 作者:行者123 更新时间:2023-12-03 08:09:53 25 4
gpt4 key购买 nike

所以我是一名正在准备期末考试的学生,我一直在从我们已经完成的其他项目中提取示例。我可以让一切正常工作,但结果是一个简单的纸石头剪刀蜥蜴斯波克游戏,但我不确定为什么结果不起作用

<html>
<head>
<script type ="text/javascript">

var gameResults // Game Results
var playerChoice // Players choice
var BR = "<br />"; // Line break
var ES = ""; // Empty space
var PA = "<p />"; // full paragraph break
var NL = "\n"; // New Line

function winResults(string)
{
gameResults = wcType;
}


function setChoice(pcType)
{
playerChoice = pcType;
}

function displayResults()
{

var name = document.RockPaperSpockForm.name.value;
var computerChoice = Math.random();
if (computerChoice < 0.2)
{
computerChoice = "Rock";
}
else if (computerChoice <= 0.4)
{
computerChoice = "Paper";
}
else if (computerChoice <= 0.6)
{
computerChoice = "Scissors";
}
else if (computerChoice <= 0.8)
{
computerChoice = "Lizard";
}
else
{
computerChoice = "Spock";
}
var compare = function(playerChoice, computerChoice)
{
if (playerChoice === computerChoice)
{
winResults(Tie);
}
else if (playerChoice === "Rock")
{
if (computerChoice === "Scissors")
{
winResults(Win);
}
else if (computerChoice === "Paper")
{
winResults(Lose);
}
else if (computerChoice === "Lizard")
{
winResults(Win);
}
else
{
winResults(Lose);
}
}
else if (playerChoice === "Paper")
{
if (computerChoice === "Scissors")
{
winResults(Lose);
}
else if (computerChoice === "Rock")
{
winResults(Win);
}
else if (computerChoice === "Lizard")
{
winResults(Lose);
}
else
{
winResults(Win);
}
}
else if (playerChoice === "Scissors")
{
if (computerChoice === "Paper")
{
winResults(Win);
}
else if (computerChoice === "Rock")
{
winResults(Lose);
}
else if (computerChoice === "Lizard")
{
winResults(Win);
}
else
{
winResults(Lose);
}
}
else if (playerChoice === "Lizard")
{
if (computerChoice === "Scissors")
{
winResults(Lose);
}
else if (computerChoice === "Rock")
{
winResults(Lose);
}
else if (computerChoice === "Paper")
{
winResults(Win);
}
else
{
winResults(Win);
}
}
else if (playerChoice === "Spock")
{
if (computerChoice === "Scissors")
{
winResults(Win);
}
else if (computerChoice === "Rock")
{
winResults(Win);
}
else if (computerChoice === "Lizard")
{
winResults(Lose);
}
else
{
winResults(Lose);
}
}
}
compare(playerChoice, computerChoice);

alert("Hello! " + name + " you have chosen " + playerChoice + " and the computer has chosen " + computerChoice + "!" + NL + "You " + gameResults + "!");
}

</script>
</head>

<body bgcolor="Azure">
<h3>Rock Paper Scissors Lizard Spock!</h3>
<form name="RockPaperSpockForm" action="">

<strong>Enter your name:</strong><br />
<input type="text" name="name" value="Name" size="40"><p />

<strong>Select Paper, Rock, Scissors, Lizard, or Spock:</strong><br />
<input type="radio" name="choice" value="Paper" onclick="setChoice(this.value)" /><img src="PaperThumb.JPG"><p />
<input type="radio" name="choice" value="Rock" onclick="setChoice(this.value)" /><img src="RockThumb.JPG"><p />
<input type="radio" name="choice" value="Scissors" onclick="setChoice(this.value)" /><img src="ScissorsThumb.JPG"><p />
<input type="radio" name="choice" value="Lizard" onclick="setChoice(this.value)" /><img src="LizardThumb.JPG"><p />
<input type="radio" name="choice" value="Spock" onclick="setChoice(this.value)" /><img src="SpockThumb.JPG"><p />

<input type="button" name="displaybutton" value="Go" onclick="displayResults()" /><p />
<textarea name="messageBox" readonly="true" value="" rows="8" cols="50"></textarea><br />

</form>
</body>
</html>

我想要的是结果将函数内的变量设置为平局、获胜、失败,然后我可以将其附加到警报,但它不起作用,显示为未定义。任何帮助将不胜感激,我被困住了。

最佳答案

如果更改这行代码:

函数 winResults(string)

阅读:

函数 winResults(wcType)

您还需要修复对 winResults 的调用,以便您传递的参数在每种情况下都是字符串文字 - 现在所有这些调用都被编写为就像传递变量一样名为WINLOSETIE。例如,您当前拥有:

winResults(平局)

您应该将其更改为:

winResults("TIE")

关于javascript - 在 JavaScript 中使用石头剪刀布蜥蜴 Spock Final 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34191249/

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