idiombook
7/16/2017 - 3:03 PM

[language: Swift, statement: conditional]

[language: Swift, statement: conditional]

enum Weather {
    case sunny
    case cloudy
    case rainy
}

func weather(with rainyPercent: Int) -> Weather? {
    if 0 <= rainyPercent && rainyPercent < 30 {
        return .sunny
    } else if 30 <= rainyPercent && rainyPercent < 70 {
        return .cloudy
    } else if 70 <= rainyPercent && rainyPercent <= 100 {
        return .rainy
    } else {
        return nil
    }
}

func weatherResult(at weather: Weather?) -> String {
    guard let weather = weather else {
        return "エラー"
    }
    switch weather {
    case .sunny:
        return "晴れ"
    case .cloudy:
        return "曇り"
    case .rainy:
        return "雨"
    }
}

print(weatherResult(at: weather(with: 29)))
print(weatherResult(at: weather(with: 60)))
print(weatherResult(at: weather(with: 90)))
print(weatherResult(at: weather(with: 200)))