我正在制作一个摇滚纸剪刀游戏。这就是我到目前为止所拥有的。我在使用函数determineOutcome时遇到问题。我试图让结果将计算机的结果恢复为摇滚,纸张或剪刀。不是randomNumber(),1,2或3.我真的很感谢你的帮助。这是我的代码:
btnPlay.addEventListener(MouseEvent.CLICK, playGame);
btnNewGame.addEventListener(MouseEvent.CLICK, newGame);
var ties:int = 0;
var wins:int = 0;
var losses:int = 0;
function playGame(e:MouseEvent):void
{
var userThrow:int;
var computerThrow:int;
var outcome:String;
userThrow = int(radRock.group.selectedData);
computerThrow = randomWholeNumber(3,1);
outcome = determineOutcome(userThrow, computerThrow);
lblOutcome.text = outcome;
lblScore.text = "Wins: " + wins.toString() + " Losses: " + losses.toString() + "Ties: " + ties.toString();
}
function newGame(e:MouseEvent):void
{
//
}
function randomWholeNumber(highNumber:int,lowNumber:int):int
{
return Math.floor((highNumber - lowNumber + 1) * Math.random() + lowNumber);
}
function determineOutcome(u:int,c:int):String
{
if(u == 2 && c == 1){
return "The computer threw a rock and your paper covered it. You Win!"
}
else if(u == 3 && c == 2){
return "The computer threw paper and your scissors cut the paper.You Win!"
}
else if(u == 1 && c == 3){
return "The computer threw scissors and your rock smashed it. You Win!"
}
else if(u == c){
return "You tied with the computer"
}
else if(u == 1 && c == 2){
return "you threw a rock and the Computer's paper covered it. You lose :("
}
else if(u == 2 && c == 3){
return "You threw paper and the Computer's scissors cut your paper.You lose :("
}
else if(u == 3 && c == 1){
return "You threw scissors and the Computer's rock smashed it. You lose :("
}
}
答案 0 :(得分:0)
此函数会抛出编译器错误,因为默认情况下它不会返回值。我的意思是在条件之外没有退货声明。如果不符合其中一个条件,您总是需要在外面使用退货声明。在最后,只需添加return"&#34 ;;或返回null;