2014年8月6日水曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 5(Making Choices)、5.6(Exercises) 11.をSwiftで考えてみる。

5.6(Exercises) 11.

コード(Xcode)

main.swift

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

import Foundation

for age:Int in 44..<47 {
    for bmi:Int in 21..<24 {
        
        let young:Bool = age < 45
        let heavy:Bool = bmi >= 22.0
        var risk:String
        
        switch (young, heavy) {
        case (true, true): risk = "medium"
        case (true, _): risk = "low"
        case(_, true): risk = "high"
        default: risk = "medium"
        }
        
        println("age \(age) bmi \(bmi): risk \(risk)")
    }
}

入出力結果(Console Output)

age 44 bmi 21: risk low
age 44 bmi 22: risk medium
age 44 bmi 23: risk medium
age 45 bmi 21: risk medium
age 45 bmi 22: risk high
age 45 bmi 23: risk high
age 46 bmi 21: risk medium
age 46 bmi 22: risk high
age 46 bmi 23: risk high
Program ended with exit code: 0

0 コメント:

コメントを投稿