2017-02-23 96 views
0
var animal:String ="Cat"; 


var isFish:Boolean; 

isFish = isItAFish(animal); 
trace(isFish); 

function isItAFish (animal:String):Boolean { 

    var fishArray:Array = new Array("haddock", "Trout", "Salmon", "Cod"); 

    for(var i:int = 0; i < fishArray.length; i++){ 

     if (fishArray[i] == animal){ 

      return true; 

      break; 
     } 
    } 
    return false; 
} 

我只是需要幫助解釋這個代碼的男孩和女孩。該「isFish = isItAFish(動物);跟蹤(isFish);?就是我從迷茫AS3新手奮鬥

+0

似乎沒什麼問題‘isItAFish’部分只調用命名方法‘isItAFish’正下方,它說「功能....」,跟蹤部分只是打印到閃光輸出窗口的值「isFish」 – mitim

+0

謝謝你。代碼? – hk111

+0

任何人????????? – hk111

回答

2
//animal is a string that contains the value "Cat" 
var animal:String ="Cat"; 

//isFish is a boolean that will be used as a flag 
var isFish:Boolean; 

//isFish value will be changed from the outcome of the function isItAFish with the animal value. 
isFish = isItAFish(animal); 
trace(isFish); 

//This function requires 1 string parameter and returns a boolean. 
function isItAFish (animal:String):Boolean 
{ 
    //fishArray is a list of all your possible fishes. 
    var fishArray:Array = new Array("haddock", "Trout", "Salmon", "Cod"); 

    /* 
    We iterate the array to see if animal ("Cat") is inside the fishArray possible values. 
    This loop will run exactly the number of times of the array's content. In this case, 4 times. 
    */ 
    for(var i:int = 0; i < fishArray.length; i++) 
    { 
     /* 
     We are comparing the values of the fishArray against animal ("Cat"). 
     fishArray[i] holds the value of the current loop count. 
     For example, the first loop will be fishArray[0] which is "haddock". 
     The 4th loop will contain the value "Cod". 
     */ 
     if (fishArray[i] == animal) 
     { 
      //If we find a match, we return 'true' and stop the loop. 
      return true; 
      break; 
     } 
    } 

    //IF the loop ends without any matches we return 'false'. 
    return false; 
} 
+0

非常感謝你 – hk111

+0

@ hk111如果它幫助確保選中標記或投票答案。 –