函数不返回值actionscript猜谜游戏

时间:2016-07-22 20:04:16

标签: actionscript-3

我对actionscript编程很新。我刚刚学习了函数,我的任务是创建一个猜谜游戏,用户输入一个范围,计算机将生成一个范围内的随机数。然后用户猜出该号码是什么。

我的主要问题是我收到了错误"函数没有返回值"对于函数guessCorrect()。我不明白为什么会发生这种情况或如何解决它。此外,如果您碰巧看到任何其他错误,请告诉我。非常感谢您的帮助。

  • 我确实尝试了返回但得到了相同的结果

这是我的代码:

btnGuess.addEventListener(MouseEvent.CLICK, guessNumbers);

//Global Variables
var count = 0;

var myGuess:int;
var Answer:int;
var highNumber:int;
var lowNumber:int;

function guessNumbers(e:MouseEvent):void {
//Set Variables
 highNumber = int(txtinHighValue.text)
 lowNumber = int(txtinLowValue.text)

Answer = randomWholeNumber(highNumber, lowNumber);

myGuess = int(txtinGuess.text);

lblOutput.text = guessCorrect(myGuess, Answer, count);

}

function randomWholeNumber(highNumber:int,lowNumber:int):int
{

return Math.floor((highNumber - lowNumber + 1) * Math.random() + lowNumber);

}

**function guessCorrect(myGuess:int, Answer:int, count:int):String {
while (myGuess != Answer) {
    if (myGuess < Answer) {
        lblOutput.text = "Guess to low - try again!"
        count ++
    }
    else if (myGuess > Answer) {
        lblOutput.text = "Guess to high - try again!"
        count ++
    }
    else if (myGuess == Answer){
        lblOutput.text = "It's a match - you win!"
        count ++
    }
}
}**

lblGuesses.text = "Guessess: " += count

btnNewGame.addEventListener(MouseEvent.CLICK, guessNumbers); {
    count = 0;
    highNumber = 0;
    lowNumber = 0;
}

1 个答案:

答案 0 :(得分:0)

如果某个功能以:void之外的其他内容结束,例如:int,则需要return行。看起来像:

function myFunction():int
{
    var myVar: int = 5;
    return myVar;
}