AS3奇数或偶数(mal)函数

问题描述 投票:0回答:2

请如何修复此AS3功能?

谢谢

function dispari(numero:int):Boolean;
{
  //check if the number is odd or even
  if (numero % 2 == 0)
  {
    returns false;
  }
  else
  {
    returns true;
  }
}

错误:1071:语法错误:属性返回后,应使用定义关键字(例如function),而不是false。

actionscript-3 flash
2个回答
6
投票

为什么在函数语句末尾有分号(;)?我没有做[[any AS3编码,但是看起来不正确,而且粗略浏览一下网络上的一些示例就没有了。

我怀疑这可能是导致您出现问题的原因。请尝试以下方法:

function dispari(numero:int):Boolean { //check if the number is odd or even if (numero % 2 == 0) { return false; } else { return true; } }

我还更改了return语句以匹配其他所有AS3的操作以返回值(谢谢,@ Herms,忘了提了:-)

5
投票
Pax是正确的答案,但是您可以通过返回结果来简化它:

function dispari(numero:int):Boolean { return (numero % 2 != 0); }

© www.soinside.com 2019 - 2024. All rights reserved.