2011-02-23 183 views
0

爲什麼此程序始終從函數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

回答

1

您正在將函數作爲參數傳遞給其他函數。您需要將函數調用的結果作爲參數傳遞給其他函數。另外,如果在字符串連接中使用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

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

你並不完全覈對參數,所以它默認爲 「未來」 每次。它適用於取代以前版本的此功能。

+0

謝謝,這比我的代碼更清潔 – dukevin 2011-03-01 08:12:19