功能返回

时间:2011-02-23 21:07:07

标签: flash string function actionscript

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

在这种情况下,b + c等于56,变量年份应该属于(b + c)> 0&& (b + c)< 1000)并返回“roman”,但它返回“future”......

如果我添加这个,我得到了这个成功的工作:

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

2 个答案:

答案 0 :(得分:2)

尝试使用此而不是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”。它可以使用此功能取代之前的版本。

答案 1 :(得分:1)

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

此外,如果在字符串连接中使用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)));