因此,我是一名学生,正在为期末考试做准备,而且我一直在从我们已经完成的其他程序中提取示例。我可以使一切正常工作,但结果却是一个简单的纸剪刀石头蜥蜴麻雀游戏,但我不确定为什么结果不起作用

<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>


我要寻找的是结果将函数内的变量设置为Tie,Win,Lose,然后我可以将其附加到警报中,但是它无法显示为未定义。任何帮助将不胜感激,我被困住了。

最佳答案

如果更改此代码行:

function winResults(string)

读书:

function winResults(wcType)

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

winResults(TIE)

您应该将其更改为:

winResults("TIE")

10-07 19:04