開発環境
- OS X Mavericks - Apple(OS)
- Xcode 6.0 Beta
- Swift (プログラミング言語)
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) 10.をSwiftで考えてみる。
5.6(Exercises) 10.
コード(Xcode)
main.swift
//
// main.swift
// ample9
//
// Created by kamimura on 8/4/14.
// Copyright (c) 2014 kamimura. All rights reserved.
//
import Foundation
func input(msg:String = "") -> String {
print(msg)
var in_fh = NSFileHandle.fileHandleWithStandardInput()
var data = in_fh.availableData
var s = NSString(data: data, encoding: NSUTF8StringEncoding)
s = s.substringToIndex(s.length - 1)
return s;
}
while true {
let s = input(msg: "phを入力: ")
if s == "" {
break
}
let ph = Float(s.toInt()!)
if ph < 7.0 {
println("It's acidic!")
}
if ph < 4.0 {
println("It's a strong acid!")
}
}
入出力結果(Console Output)
phを入力: 0 It's acidic! It's a strong acid! phを入力: 1 It's acidic! It's a strong acid! phを入力: 2 It's acidic! It's a strong acid! phを入力: 3 It's acidic! It's a strong acid! phを入力: 4 It's acidic! phを入力: 5 It's acidic! phを入力: 6 It's acidic! phを入力: 7 phを入力: 8 phを入力: 9 phを入力: 10 phを入力: Program ended with exit code: 0
0 コメント:
コメントを投稿