close
 
變數
 
var myAge : Int = 32
myAge = 33

print(myAge)

let myName = "Shih-Wu"
//使用let的變數一經指定後不允許修改,跟JS6定義(const)不同
//myName = "ERROR"

let myAgeInTenYear = myAge + 10

let myFullName = myName + " Cheng"
 
//字串中加入變數
let myDetails = "\(myName), \(myAge)"

let wholeNumber : Int = 12

let text : String = "abc"

let booleans: Bool = false

let floatingPointNum : Float = 1.311111

let doubleNum : Double = 3.14
 
Function 函數
沒有回傳值
func getMilk(){}
 
沒有回傳值,有輸入整數
func getMilk(howManyMilkCartons : Int){}
呼叫方式
getMilk(howManyMilkCartons: 3)
 
有回傳值
func getMilk(howManyMilkCartons : Int) -> Int{
    return howManyMilkCartons * 6
}
呼叫方式
getMilk(howManyMilkCartons: 3)
 
控制 conditional
func loveCalulator(yourName: String, hisName: String) -> String{
    
    let loveScore = Int(arc4random_uniform(101))
    
    if loveScore > 80 {
        return "Score:\(loveScore). You love each other likes...."
    }
    else if loveScore > 40 && loveScore <= 80 {
        return "Score:\(loveScore). Together possible...."
    }
    else {
        return "Score:\(loveScore). No love possible...."
    }
    //return loveScore
}

loveCalulator(yourName: "45", hisName: "56")
 
FOR迴圈
 
指定1~10 只有當數字%2=0才計算總合
var sum2 = 0;
for xxx in 1...10 where xxx % 2 == 0{
    sum2 += xxx
}

print(sum2)
 
 
指定整數陣列,計算陣列中的整數合
var arrayOfNumbers = [1,3,5,7,9,2,4,6,8,0]

var sum = 0

for num in arrayOfNumbers {
    print(num)
    
    sum += num
}

print("sum = \(sum)")
 
 
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 45 的頭像
    45

    FIVE

    45 發表在 痞客邦 留言(0) 人氣()