简体   繁体   English

函数返回

[英]Function returning

Why does this program always output the text for "future" from function year()? 为什么此程序总是从函数year()输出“未来”的文本?

In this case, with b+c equaling 56, var year should fall under (b+c) > 0 && (b+c) < 1000) and return "roman", but it instead returns "future"... 在这种情况下,如果b + c等于56,则var year应该低于(b + c)> 0 &&(b + c)<1000),然后返回“罗马”,但返回“未来” ...

I got this to successfully work if I add this: 如果添加以下内容,则可以成功工作:

var period:String = (year(b,c));

and in my body function, make the conditionals check the period. 在我的身体功能中,让条件检查时间段。 For example 例如

if (period == "future")

but I don't understand why I need to do this. 但我不明白为什么我需要这样做。 I am returning a string, why do I have to set another variable? 我正在返回一个字符串,为什么我必须设置另一个变量? There is no compiler errors so clearly its not syntactical? 没有编译器错误,因此很明显它不是语法吗?

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

Try this instead of your if/else structure: 试试这个代替您的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;
    }
}

You weren't exactly checking against the parameter, so it defaulted to "future" every time. 您没有完全检查该参数,因此每次都默认将其设置为“ future”。 It works with this function supplanting the previous version. 它可以代替以前的版本使用此功能。

You are passing functions as parameters to other functions. 您正在将函数作为参数传递给其他函数。 You need to pass the result of a function call as parameters to the other functions. 您需要将函数调用的结果作为参数传递给其他函数。

Also, if you use int+int in a string concatination, you need to put that calculation between brackets. 另外,如果在字符串中使用int + int,则需要将该计算放在方括号之间。 So use (int+int) instead. 因此,请改用(int + int)。

In the intro function, you passed in the sum as a parameter, but did not use it. 在介绍函数中,您传入了sum作为参数,但没有使用它。 Instead you recalculated b+c. 相反,您重新计算了b + c。

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM