函数返回

问题描述:

为什么此程序始终从函数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)); 
+0

在干净的FLA中粘贴代码会得到年份函数的“roman”(带参数50和6)。你确定别的东西没有用吗? – 2011-02-23 21:39:27

+0

如果我将代码完全粘贴到闪存中,我会得到“曾几何时,在506年,凯尔和蒂姆看到一个飞碟,并决定他们想成为一名外星人。” – dukevin 2011-02-23 22:03:58

您正在将函数作为参数传递给其他函数。您需要将函数调用的结果作为参数传递给其他函数。另外,如果在字符串连接中使用int + int,则需要在括号内放置该计算。所以用(int + int)代替。

在介绍函数中,您传入的总和作为参数,但未使用它。相反,你重新计算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))); 
+0

你很高兴为他解决问题,但你应该添加一些你改变的解释 - 否则别人很难跟随这个问题和答案。 – weltraumpirat 2011-02-23 22:20:41

+0

对,我先添加了解决方案,稍后再添加评论。你必须在两者之间阅读答案。 – 2011-02-23 22:22:00

+0

我用diff来查看差异;帮了很多!谢谢 – dukevin 2011-03-01 08:11:19

尝试,而不是你的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; 
    } 
} 

你并不完全核对参数,所以它默认为 “未来” 每次。它适用于取代以前版本的此功能。

+0

谢谢,这比我的代码更清洁 – dukevin 2011-03-01 08:12:19