Flash 函数返回

Flash 函数返回,flash,string,function,actionscript,Flash,String,Function,Actionscript,为什么此程序总是从函数year()输出“future”的文本 在这种情况下,当b+c等于56时,var年应在(b+c)>0&(b+c)2000) 回归“未来”; 如果((b+c)>1000和&b+c0&(b+c)1000和&b+c0&(b+c)1000和&b+c0&(b+c)

为什么此程序总是从函数year()输出“future”的文本

在这种情况下,当b+c等于56时,var年应在(b+c)>0&(b+c)<1000)下,并返回“罗马”,但它返回“未来”

如果我添加以下内容,我将成功实现此功能:

var period:String = (year(b,c));
在我的身体功能中,让条件句检查句点。比如说

if (period == "future")
但我不明白我为什么要这么做。我返回一个字符串,为什么我必须设置另一个变量?没有编译器错误那么明显它不是语法的

var a:String = "Tim";
var b:int = 50; //CHANGE TO ANY INT YOU WANT
var c:int = 6; //CHANGE TO ANY INT YOU WANT
var d:String = "Kyle";
var sum:int = b+c;

function friend(d:String, a:String):String
{
    return d+" and "+a;
}

function year(b:int, c:int):String
{
    if( (b+c) > 2000 )
        return "future";
    else if( (b+c)> 1000 && b+c< 2000)
        return "colonial";
    else if( (b+c) > 0 && (b+c) < 1000)
        return "roman";
    else if( (b+c) < 0)
        return "medieval";
    else
        return "fail";

}


function intro(sum, friend):String
{
    return "Once upon a time, in the year "+ b+c +", "+friend;
}

function body(year):String
{
    if ("future")
        return " saw a flying saucer and descided they wanted do be an alien.";
    else if ("colonial")
        return " just got off the the Mayflower and descided they wanted to eat some turkey.";
    else if ("roman")
        return " are taking a break after a fierce battle with the Romans.";
    else if ("medieval")
        return " saved the princess in shining armor after slaying the dragon.";
    else if ("fail")
        return " just got an F on their exam.";
    else
        return " just got an F on their test.";            
}
trace (b+c);
trace(intro(sum, friend(d, a)) + body(year));
var a:String=“Tim”;
变量b:int=50//更改为您想要的任何整数
变量c:int=6//更改为您想要的任何整数
变量d:String=“Kyle”;
var总和:int=b+c;
函数友元(d:String,a:String):String
{
返回d+“和”+a;
}
函数年(b:int,c:int):字符串
{
如果((b+c)>2000)
回归“未来”;
如果((b+c)>1000和&b+c<2000),则为其他情况
回归“殖民地”;
如果((b+c)>0&(b+c)<1000,则为其他情况
返回“罗马”;
如果((b+c)<0),则为
返回“中世纪”;
其他的
返回“失败”;
}
函数简介(和,朋友):字符串
{
返回“从前,在这一年”+b+c+,“+朋友;
}
函数体(年份):字符串
{
如果(“未来”)
return“看到一个飞碟,并说他们想成为外星人。”;
else if(“殖民地”)
return“刚从五月花号上下来,说他们想吃火鸡。”;
else if(“罗马”)
return“在与罗马人的激烈战斗后,他们正在休息。”;
else if(“中世纪”)
return“杀死龙后,她用闪亮的盔甲拯救了公主。”;
否则,如果(“失败”)
return“他们的考试刚刚得了F。”;
其他的
return“他们的测试刚刚得了F。”;
}
微量元素(b+c);
跟踪(简介(总和,朋友(d,a))+正文(年份));

您正在将函数作为参数传递给其他函数。您需要将函数调用的结果作为参数传递给其他函数

此外,如果在字符串表达式中使用int+int,则需要将该计算放在括号之间。所以用(int+int)代替

在intro函数中,您将sum作为参数传入,但没有使用它。相反,你重新计算了b+c

试试这个:

var a:String = "Tim";
var b:int = 50; //CHANGE TO ANY INT YOU WANT
var c:int = 6; //CHANGE TO ANY INT YOU WANT
var d:String = "Kyle";
var sum:int = b+c;

function friend(d:String, a:String):String
{
    return d+" and "+a;
}

function year(b:int, c:int):String
{
    if( (b+c) > 2000 )
        return "future";
    else if( (b+c)> 1000 && b+c< 2000)
        return "colonial";
    else if( (b+c) > 0 && (b+c) < 1000)
        return "roman";
    else if( (b+c) < 0)
        return "medieval";
    else
        return "fail";

}


function intro(sum:int, friend:String):String
{
    return "Once upon a time, in the year "+ sum +", "+friend;
}

function body(year:String):String
{
    if ("future")
        return " saw a flying saucer and descided they wanted do be an alien.";
    else if ("colonial")
        return " just got off the the Mayflower and descided they wanted to eat some turkey.";
    else if ("roman")
        return " are taking a break after a fierce battle with the Romans.";
    else if ("medieval")
        return " saved the princess in shining armor after slaying the dragon.";
    else if ("fail")
        return " just got an F on their exam.";
    else
        return " just got an F on their test.";            
}
trace (b+c);
trace(intro(sum, friend(d, a)) + body(year(b, c)));
var a:String=“Tim”;
变量b:int=50//更改为您想要的任何整数
变量c:int=6//更改为您想要的任何整数
变量d:String=“Kyle”;
var总和:int=b+c;
函数友元(d:String,a:String):String
{
返回d+“和”+a;
}
函数年(b:int,c:int):字符串
{
如果((b+c)>2000)
回归“未来”;
如果((b+c)>1000和&b+c<2000),则为其他情况
回归“殖民地”;
如果((b+c)>0&(b+c)<1000,则为其他情况
返回“罗马”;
如果((b+c)<0),则为
返回“中世纪”;
其他的
返回“失败”;
}
函数简介(sum:int,friend:String):String
{
返回“从前,在一年中”+总和+,“+朋友;
}
函数体(年份:字符串):字符串
{
如果(“未来”)
return“看到一个飞碟,并说他们想成为外星人。”;
else if(“殖民地”)
return“刚从五月花号上下来,说他们想吃火鸡。”;
else if(“罗马”)
return“在与罗马人的激烈战斗后,他们正在休息。”;
else if(“中世纪”)
return“杀死龙后,她用闪亮的盔甲拯救了公主。”;
否则,如果(“失败”)
return“他们的考试刚刚得了F。”;
其他的
return“他们的测试刚刚得了F。”;
}
微量元素(b+c);
trace(介绍(sum,friend(d,a))+正文(年(b,c));

您正在将函数作为参数传递给其他函数。您需要将函数调用的结果作为参数传递给其他函数

此外,如果在字符串表达式中使用int+int,则需要将该计算放在括号之间。所以用(int+int)代替

在intro函数中,您将sum作为参数传入,但没有使用它。相反,你重新计算了b+c

试试这个:

var a:String = "Tim";
var b:int = 50; //CHANGE TO ANY INT YOU WANT
var c:int = 6; //CHANGE TO ANY INT YOU WANT
var d:String = "Kyle";
var sum:int = b+c;

function friend(d:String, a:String):String
{
    return d+" and "+a;
}

function year(b:int, c:int):String
{
    if( (b+c) > 2000 )
        return "future";
    else if( (b+c)> 1000 && b+c< 2000)
        return "colonial";
    else if( (b+c) > 0 && (b+c) < 1000)
        return "roman";
    else if( (b+c) < 0)
        return "medieval";
    else
        return "fail";

}


function intro(sum:int, friend:String):String
{
    return "Once upon a time, in the year "+ sum +", "+friend;
}

function body(year:String):String
{
    if ("future")
        return " saw a flying saucer and descided they wanted do be an alien.";
    else if ("colonial")
        return " just got off the the Mayflower and descided they wanted to eat some turkey.";
    else if ("roman")
        return " are taking a break after a fierce battle with the Romans.";
    else if ("medieval")
        return " saved the princess in shining armor after slaying the dragon.";
    else if ("fail")
        return " just got an F on their exam.";
    else
        return " just got an F on their test.";            
}
trace (b+c);
trace(intro(sum, friend(d, a)) + body(year(b, c)));
var a:String=“Tim”;
变量b:int=50//更改为您想要的任何整数
变量c:int=6//更改为您想要的任何整数
变量d:String=“Kyle”;
var总和:int=b+c;
函数友元(d:String,a:String):String
{
返回d+“和”+a;
}
函数年(b:int,c:int):字符串
{
如果((b+c)>2000)
回归“未来”;
如果((b+c)>1000和&b+c<2000),则为其他情况
回归“殖民地”;
如果((b+c)>0&(b+c)<1000,则为其他情况
返回“罗马”;
如果((b+c)<0),则为
返回“中世纪”;
其他的
返回“失败”;
}
函数简介(sum:int,friend:String):String
{
返回“从前,在一年中”+总和+,“+朋友;
}
函数体(年份:字符串):字符串
{
如果(“未来”)
return“看到一个飞碟,并说他们想成为外星人。”;
else if(“殖民地”)
return“刚从五月花号上下来,说他们想吃火鸡。”;
else if(“罗马”)
return“在与罗马人的激烈战斗后,他们正在休息。”;
else if(“中世纪”)
return“杀死龙后,她用闪亮的盔甲拯救了公主。”;
否则,如果(“失败”)
return“他们的考试刚刚得了F。”;
其他的
return“他们的测试刚刚得了F。”;
}
微量元素(b+c);
trace(介绍(sum,friend(d,a))+正文(年(b,c));

尝试以下方法,而不是使用if/else结构:

function body(year:String):String
{
    switch(year)
    {
        case "future":
        return " saw a flying saucer and descided they wanted do be an alien.";
        break;
        case "colonial":
        return " just got off the the Mayflower and descided they wanted to eat some turkey.";
        break;
        case "roman":
        return " are taking a break after a fierce battle with the Romans.";
        break;
        case "medieval":
        return " saved the princess in shining armor after slaying the dragon.";
        break;
        case "fail":
        return " just got an F on their exam.";
        break;
        default:
        return " just got an F on their test."; 
        break;
    }
}

您没有严格检查参数,因此每次都默认为“future”。它使用此函数取代以前的版本。

尝试使用此函数,而不是使用if/else结构:

function body(year:String):String
{
    switch(year)
    {
        case "future":
        return " saw a flying saucer and descided they wanted do be an alien.";
        break;
        case "colonial":
        return " just got off the the Mayflower and descided they wanted to eat some turkey.";
        break;
        case "roman":
        return " are taking a break after a fierce battle with the Romans.";
        break;
        case "medieval":
        return " saved the princess in shining armor after slaying the dragon.";
        break;
        case "fail":
        return " just got an F on their exam.";
        break;
        default:
        return " just got an F on their test."; 
        break;
    }
}

您没有严格检查参数,因此每次都默认为“future”。它使用此函数取代以前的版本。

将代码粘贴到干净的FLA中会产生年度函数的
“roman”
(参数为50和6)。你确定没有其他东西在起作用吗?如果我把代码复制粘贴到flash中,我会得到“从前,在506年,