2014年8月15日金曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 5(Understanding Objects: A trip to Objectville)、SHARPEN YOUR PENCIL(p.205)をSwiftで考えてみる。

SHARPEN YOUR PENCIL(p.205)

コード(Xcode)

main.swift

//
//  main.swift
//  sample205
//
//  Created by kamimura on 8/15/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

class EightBall {
    var index:Int = 0
    let advice:[String] = ["yes", "no", "maybe", "not a chance"]
    
    func shake () {
        self.index += 1
        if self.index >= self.advice.count {
            self.index = 0
        }
    }
    func look () -> String {
        return self.advice[self.index]
    }
}

let eightBall = EightBall()

eightBall.shake()
println(eightBall.look())
eightBall.shake()
println(eightBall.look())
eightBall.shake()
println(eightBall.look())
eightBall.shake()
println(eightBall.look())
eightBall.shake()
println(eightBall.look())

入出力結果(Console Output)

no
maybe
not a chance
yes
no
Program ended with exit code: 0

0 コメント:

コメントを投稿