0 && (b+c) < 1000) and return \"ro" />
开发者

Function returning

Why does this program always output the text for "future" from function 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"...

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 ("futur开发者_开发问答e")
        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:

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. 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. So use (int+int) instead.

In the intro function, you passed in the sum as a parameter, but did not use it. Instead you recalculated 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)));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜