Swift学习日记002

以学习日记001里面知识点为基础,构建第一个命令行的swift工程,功能很简单,生成一个随机数(0-100范围内),输入后进行各种判读输出,代码如下:

所需的算法文件链接:http://www.raywenderlich.com/wp-content/uploads/2014/10/helpers.swift_.zip


import Foundation

//随机数
let answer = randomIntBetween(0, high: 100)

//输入次数
var turn = 1

while (true){
    print("Enter a number between 1 and 100 .")
    
    let userInput = input()
    
    let inputAsInt = Int(userInput)
    
    if let guess = inputAsInt{
        if(guess > answer) {
            print("Lower!")
        } else if(guess < answer) {
            print("Higher!")
        } else {
            print("Correct! The answer was \(answer).")
            break;
        }
    }else{
        print("Invalid input! Please enter a number.")
    }
    turn += 1
}

print("It took you \(turn) times to find right answer!")



你可能感兴趣的:(iOS学习,Swift)